我正在从 jQuery 1.3.2 升级到 jQuery 1.5,但无法解析 JSON。使用 $.ajax,我调用一个 php 脚本,该脚本通过 PHP json_encode 方法返回以下内容:
{"vErrorsFound":true,"vMessage":"Login Failed"}
我尝试将对象简化为:
单独使用 {"vErrorsFound":true}
和单独使用 {"vMessage":"Login Failed"}
但我仍然从 ajax 调用中收到解析错误。
这是 ajax 调用。
$.ajax({
url : "index_vld.php",
data: {Username: $("input[name=Username]").val(), Password: $("input[name=Password]").val()},
dataType: 'json',
success: function (data, textStatus, jqXHR)
{
alert(data);
},
error: function (jqXHR, textStatus, errorThrown)
{
alert(textStatus + ' ' + errorThrown);
}
});
我发现 JSON 解析在较新版本的 jquery 中更加严格,但是我无法找出从 PHP 脚本返回的这个简单 json 对象中有什么问题。我还尝试将 php 脚本的内容类型设置为 application/json 但没有成功。
抛出的异常如下: jQuery15045727721066214144_1298305275108 未被调用。
在解决这个问题之前无法升级到 jQuery 1.5。
感谢您的帮助。
最佳答案
http://blog.m0sa.net/2011/02/jqueryvalidation-breaks-jquery-15-ajax.html
The cause of the problem is this line of JavaScript in jQuery.validate.js, that overrides the settings you pass into the $.ajax call with all the default ones (and jQuery.ajaxSettings defaults to { jsonp: "callback", jsonpCallback: function() {...}}):
解决方法:
$(function () {
$.ajaxSettings.cache = false;
$.ajaxSettings.jsonp = undefined;
$.ajaxSettings.jsonpCallback = undefined;
})
关于jquery - $.ajax 解析错误 {"vErrorsFound":true ,"vMessage" :"Login Failed"},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5068822/