这是我想要实现的部分示例。
我正在尝试从 ajax 检索一个值,但 javascript result.success 变量未定义。我有以下内容:
PHP:
$result = array ("success" => null, "html" => "");
$result['success'] = true;
$result['html'] = "test";
echo json_encode($result);
Javascript/jQuery:
var ID = 1
$.ajax({
type: 'POST',
url: '/ajax/load.php',
contentType: "application/json; charset=utf-8",
datatype: 'json',
data: {ID: ID},
beforeSend: function(xhrObj) {
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Accept","application/json");
},
success: function(result) {
if (result.success) {
// do something
}
});
我从 ajax 得到的响应(从 chrome 开发工具中检索)是 {"success":true,"html":"Test"}
这对我来说看起来不错,但是在 JavaScript 中 result.success 是未定义的。我相信这很简单,我只是看不出问题出在哪里..