ajax - Pass object from javascript to Perl dancer framework -
i have following ajax
code pass values dancer framework
.
booksave: function(data) { ### data object contain more 1 key value pair var book = book.code; $.ajax ({ type: "get", url : 'textbook/save/' + book + '/' + data, success: function(data) { if(data.status == 1) { alert("success"); } else { alert("fail"); } }, }); },
in dancer:
any [ 'ajax', 'get' ] => '/save/:book/:data' => sub { set serializer => 'json'; $book = params->{book}; $data = params->{data}; ## getting object object instead of hash };
is there way pass object js , getting hash in dancer?
first , foremost, consider using http put or post verbs, , not get. not doing more semantically correct, allows include more complex objects in http body, such 'data' hash (serialized, per comments below).
i've had limited success dancer's native ajaxy methods, plus there bug causes problems in versions of firefox. instead, serialize , deserialize json object.
suggested changes (note suggested changes routes well):
$.ajax ({ type: "put", url : '/textbook/' + book, data: { myhash : json.stringify(data) }, datatype: 'json', contenttype: 'application/json', success: function (response) { if (response.status == 1) { alert("success") } else { alert("fail") } } })
and perl dancer code changes follows:
any [ 'ajax', 'put' ] => '/textbook/:book' => sub { set serializer => 'json'; $book = param('book'); $data = from_json(param('myhash')); };
i did not go far testing code, should @ least give starting point finish solving problem.
good luck project!
Comments
Post a Comment