开发者

JQuery .post not working, doesn't seem to be loading post page

This is confusing... I do not understand why this is not working... seems pretty straight forward:

var _t = $('#newbook-title').serialize();
var _a = $('#newbook-author').serialize();
var _u = $('#newbook-authorurl').serialize();
var _w = $('#newbook-why').serialize();

$.post("newbook.php", {t: _t, a: _a, u: _u, w: _w}, function(data) {
    alert( data.status + ',' + data.message);
});

Then in my newbook.php:

<?php
    echo "{\"status\": \"false\",开发者_JAVA百科 \"message\":\"Made it here.\"}";
    return;
?>

The alert is always undefined,undefined Why? I also tried .val() instead of .serialize()


Probably because the content is not being interpreted as JSON. Tell $.post what format you expect to receive:

$.post("newbook.php", {t: _t, a: _a, u: _u, w: _w}, function(data) {
    alert( data.status + ',' + data.message);
}, 'json');

Another option is to send a JSON content-type header, which jQuery will interpret for you. This is the more standards-compliant, "correct" approach:

// before content is output in your PHP script
header('Content-type: application/json');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜