开发者

JQuery Handling JSON or Array

开发者 https://www.devze.com 2023-01-29 18:30 出处:网络
Hey guys, I\'m having a problem handling my callback data in JQuery. The following is my AJAX: $(\".ajaxPostMessage\").submit(function() {

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消