即使我的 JSON 在 jsonlint.com 上验证,我仍收到 ParseError。
这是 jQuery 代码:
$.ajax({
url: path,
type: 'GET',
data: {},
cache: false,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
var a = 'breakpoint here doesn't activate';
},
error: function (x, y, z) {
var b = 'code execution stops at a breakpoint here.';
}
});
这是正在调用的 PHP 代码:
function getAllAnswersToHitViaAjax($theJobName) {
$testData[0] = 'testing123';
$encodedData = json_encode($testData);
echo $encodedData;
return;
}
这又回到了我的 .ajax 调用的 error: 函数中设置的断点。参数 Y 设置为“parseerror”,并且 x.responseText =
["testing123"]
到目前为止,我已经研究这个问题几个小时了。我查看了许多相关的 StackOverflow 帖子,但没有一个提供适用于这种情况的解决方案。
如何从此 .ajax 调用获得成功响应?
提前非常感谢所有人提供的任何信息。
最佳答案
您的代码没有任何明显的错误,当我在本地计算机上尝试时它工作正常。然而,您上面的评论是一个很大的线索:
I just looked at the z param in the Safari console, and found this:"undefined is not a function (evaluating 'JSON.parse(a+"")')" How could that be happening?
如果某处的某些代码使用“JSON”作为全局变量名称,隐藏内置的 window.JSON 对象,则可能会发生这种情况。
关于javascript - 即使 JSON 看起来正确,jQuery .ajax 调用也会返回 JSON 解析错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28469990/