升级到jQuery 1.5.2后,我开始在返回json数据时出现ajax调用问题。
错误是(由下面的templateGet()返回):
Ajax call failed: [object object]parsererrorjQuery152040843801534161517_1302269320612未被调用
下面是一个示例返回json:
{"subject":"Test subject","body":"Test body"}
这里是jQuery函数
function ajax_templateGet(templateid) {
showLoading();
var query = '?action=get_template' + '&templateid=' + templateid;
$.ajax({
type: 'POST',
url: 'script/ajax/mail_template/mail_template.ashx' + query,
data: '',
dataType: 'json',
success: function(data) {
$("#preview_subject").empty().html(data.subject);
$("#preview_body").empty().html(data.body);
},
error: function(xhr, status, error) {
$.jGrowl($.i18n._('Ajax call failed: ' + xhr + ' ' + status + " " + error), { header: $.i18n._('Ajax call failed!') });
},
complete: function(jqXHR, textStatus) {
hideLoading();
}
});
}
有人能看出我做错了什么吗?
您是否使用验证插件?如果是这样,确保你得到一个新的与1.5相匹配的副本-这是一个已知的问题,我也遇到过。
https://github.com/jzaefferer/jquery-validation首先需要解析返回的JSON值....
不能马上使用data.subject
首先你需要下载json2.js文件并添加到你的应用程序中。
然后解析变量data
var response=eval("("+JSON.stringify(data)+")");
然后在你发布的代码中使用变量response
而不是data
success: function(data) {
var response=eval("("+JSON.stringify(data)+")");
$("#preview_subject").empty().html(response.subject);
$("#preview_body").empty().html(response.body);
}
在jquery1.5.2.js中找到以下行:
d.ajaxPrefilter("json jsonp", function (b, c, e)
改为
d.ajaxPrefilter("jsonp", function (b, c, e)
行得通,所有的$。Ajax函数又高兴了。
来源::http://debeerdev.wordpress.com/2011/04/13/jquery-1-5-2-json/