简体   繁体   English

jQuery .post不起作用,似乎未加载帖子页面

[英]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: 然后在我的newbook.php中:

<?php
    echo "{\"status\": \"false\", \"message\":\"Made it here.\"}";
    return;
?>

The alert is always undefined,undefined Why? 警报总是不确定的,为什么? I also tried .val() instead of .serialize() 我还尝试了.val()而不是.serialize()

Probably because the content is not being interpreted as JSON. 可能是因为内容没有被解释为JSON。 Tell $.post what format you expect to receive: 告诉$.post您希望接收哪种格式:

$.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. 另一个选择是发送JSON内容类型标头,jQuery会为您解释。 This is the more standards-compliant, "correct" approach: 这是更符合标准的“正确”方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM