Hey guys, I'm having a problem handling my callback data in JQuery. The following is my AJAX:
$(".ajaxPostMessage").submit(function() {
var action = $(this).attr('action');
$.post(action, $(this).serialize(), function(data) {
alert(data);
});
return false;
});
My PHP goes something like..
echo json_e开发者_StackOverflow社区ncode(array('result'=>1, 'msg'=>'message here'));
I can't seem to get the data.result or data.msg to print, I get 'undefined'. I'm wondering if I have to also pass the post as JSON? But that shouldn't matter.. I have also tried $.parseJSON but to no avail!
Try using:
$.post(action, $(this).serialize(), function(data) {
alert(data);
},'json');
This tells jQuery your returned data is json
data
will be a string.
You can call $.parseJSON
to parse it as JSON.
精彩评论