【发布时间】:2017-01-07 14:11:03
【问题描述】:
我正在尝试将一个字符串化的 json 对象发送到 php 并在我对其进行解码后在那里访问它,但我无法做到这一点,我在 php 中收到的是一个 null ... 一些东西:
JS:
var bruh = {id: "509", type: "14", sortorder: "0", status: "1", name: "Pop-up SaaS", title: "test", request: true};
serializedData = JSON.stringify(bruh);
request = $.ajax({
url: url,
type: "POST",
dataType: "JSON",
data: serializedData
});
request.done(function (response, textStatus, jqXHR){
console.log("Hooray, it worked!"+response, textStatus, jqXHR);
});
PHP:
header('Content-Type: application/json');
$article = json_decode($_POST['data']); // $_POST['data'] is null don't know why
我怎么知道 $_POST 为空?好吧,我正在输出结果并检查我的 console.log 上的响应,如下所示:
echo json_encode($_POST['data']);
echo json_encode($_POST);
echo json_encode($_POST[0]);
echo $_POST['data'];
print_r($_POST); //SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
我收到的确切响应是(通过 Firebug):
readyState 4
responseJSON null
responseText "null"
status 200
statusText "OK"
谁能帮我解决这个问题?
更新:我也尝试过这样发送:
serializedData = "{data:"+ JSON.stringify(bruh)+"}";
还是没有结果
【问题讨论】:
-
您的 POST 中没有
data密钥。真正应该检查的是encodings而不是所有这些print_r($_POST) -
如果我这样做的话,我会收到 SyntaxError: JSON.parse: unexpected character at line 1 column 1 JSON data
-
你需要检查你的php脚本的OUTPUT。
-
如果我这样做??你怎么不这样做那个的方式,只是做u_mulder的方式,使用第一个js代码
-
@u_mulder ,我到底是怎么做到的?
标签: php arrays json ajax object