$.getJSON()不适用于jQuery 1.5中的ASP.NET MVC2
我正在尝试新的jQuery1.5,它破坏了我应用程序中的一些东西。我调用了一个生成JSON的操作,但出现了一些错误并导致脚本停止。根据Fiddler和Firebug的说法,该操作确实返回JSON数据。我没有提供JSON数据,但根据JSONLint,数据是有效的 请注意,这与jQuery 1.4.4中的预期一样有效 我注意到的第一件事是URL:http://localhost:3219/News/GetAllNewsArchives?callback=jQuery15033185029088076134_1296751219270&_=1296751219672 脚本:$.getJSON()不适用于jQuery 1.5中的ASP.NET MVC2,json,asp.net-mvc-2,jquery-1.5,Json,Asp.net Mvc 2,Jquery 1.5,我正在尝试新的jQuery1.5,它破坏了我应用程序中的一些东西。我调用了一个生成JSON的操作,但出现了一些错误并导致脚本停止。根据Fiddler和Firebug的说法,该操作确实返回JSON数据。我没有提供JSON数据,但根据JSONLint,数据是有效的 请注意,这与jQuery 1.4.4中的预期一样有效 我注意到的第一件事是URL:http://localhost:3219/News/GetAllNewsArchives?callback=jQuery15033185029088076
// Dropdown box for past articles
$("#article-select").ready(function() {
$.ajaxSetup({ cache: false });
$.getJSON('/News/GetAllNewsArchives', null, function(json) {
var items = "<option value=''>(Select)</option>";
$.each(json, function(i, item) {
items += "<option value='" + item.Id + "'>" + subject + "</option>";
});
$("#article-select").html(items);
});
});
你知道我做错了什么吗 出于某种原因,它将您的请求解释为JSONP。在Firebug中,检查$.ajaxSettings的值,确保没有将其数据类型默认为jsonp
您是否尝试过直接使用$.ajax来显式设置请求的类型、数据类型等?出于某种原因,它将您的请求解释为JSONP。在Firebug中,检查$.ajaxSettings的值,确保没有将其数据类型默认为jsonp
您是否尝试过直接使用$.ajax来显式设置请求的类型、数据类型等?好的,我切换到$.ajax调用,出现了相同的错误:
// Dropdown box for past articles
$("#article-select").ready(function() {
$.ajax({
cache: false,
type: "POST",
dataType: "json",
url: "/News/GetAllNewsArchives",
success: function(json) {
var items = "<option value=''>(Select)</option>";
$.each(json, function(i, item) {
items += "<option value='" + item.Id + "'>" + subject + "</option>";
});
$("#article-select").html(items);
}
});
然而,我注意到了屏幕上的一些东西
从jQuery1.5开始,jQuery可以将它在内容类型头中接收到的数据类型转换为您需要的数据类型。例如,如果希望将文本响应视为XML,请使用文本XML作为数据类型。您还可以发出JSONP请求,将其作为文本接收,并由jQuery将其解释为XML:JSONPTextXML。类似地,像jsonp xml这样的速记字符串将首先尝试从jsonp转换为xml,如果转换失败,将从jsonp转换为文本,然后从文本转换为xml
我将数据类型从dataType:json更改为dataType:text-json,然后它就工作了
现在,我只是不明白为什么会有不同。json期望有什么不同呢?好的,我切换到了$.ajax调用,出现了相同的错误:
// Dropdown box for past articles
$("#article-select").ready(function() {
$.ajax({
cache: false,
type: "POST",
dataType: "json",
url: "/News/GetAllNewsArchives",
success: function(json) {
var items = "<option value=''>(Select)</option>";
$.each(json, function(i, item) {
items += "<option value='" + item.Id + "'>" + subject + "</option>";
});
$("#article-select").html(items);
}
});
然而,我注意到了屏幕上的一些东西
从jQuery1.5开始,jQuery可以将它在内容类型头中接收到的数据类型转换为您需要的数据类型。例如,如果希望将文本响应视为XML,请使用文本XML作为数据类型。您还可以发出JSONP请求,将其作为文本接收,并由jQuery将其解释为XML:JSONPTextXML。类似地,像jsonp xml这样的速记字符串将首先尝试从jsonp转换为xml,如果转换失败,将从jsonp转换为文本,然后从文本转换为xml
我将数据类型从dataType:json更改为dataType:text-json,然后它就工作了
现在,我只是不明白为什么会有不同。json期望有什么不同?响应中的内容类型是什么?不是您要求的内容,也不是在数据类型parm上指定的内容,而是服务器从fiddler返回的响应内容类型是什么?不是您要求的内容,也不是在数据类型parm上指定的内容,而是服务器作为来自fiddler的响应的内容类型发回的内容类型。在jquery 1.5源代码中,这是检测内容类型的代码
// Remove auto dataType and get content-type in the process
while( dataTypes[ 0 ] === "*" ) {
dataTypes.shift();
if ( ct === undefined ) {
ct = jXHR.getResponseHeader( "content-type" );
}
}
// Check if we're dealing with a known content-type
if ( ct ) {
for ( type in contents ) {
if ( contents[ type ] && contents[ type ].test( ct ) ) {
dataTypes.unshift( type );
break;
}
}
如果将数据类型设置为json,那么前面的代码将使ct保持未定义状态。
这就是为什么它不能像预期的那样工作。
这在jQuery1.5中是一个问题,因为它破坏了与以前版本jQuery的兼容性
因此,您应该将dataType设置为text json或删除dataType,以便使用默认值
默认值:
converters: {
// Convert anything to text
"* text": window.String,
// Text to html (true = no transformation)
"text html": true,
// Evaluate text as a json expression
"text json": jQuery.parseJSON,
// Parse text as xml
"text xml": jQuery.parseXML
}
在jQuery1.5源代码中,这是检测内容类型的代码
// Remove auto dataType and get content-type in the process
while( dataTypes[ 0 ] === "*" ) {
dataTypes.shift();
if ( ct === undefined ) {
ct = jXHR.getResponseHeader( "content-type" );
}
}
// Check if we're dealing with a known content-type
if ( ct ) {
for ( type in contents ) {
if ( contents[ type ] && contents[ type ].test( ct ) ) {
dataTypes.unshift( type );
break;
}
}
如果将数据类型设置为json,那么前面的代码将使ct保持未定义状态。
这就是为什么它不能像预期的那样工作。
这在jQuery1.5中是一个问题,因为它破坏了与以前版本jQuery的兼容性
因此,您应该将dataType设置为text json或删除dataType,以便使用默认值
默认值:
converters: {
// Convert anything to text
"* text": window.String,
// Text to html (true = no transformation)
"text html": true,
// Evaluate text as a json expression
"text json": jQuery.parseJSON,
// Parse text as xml
"text xml": jQuery.parseXML
}
这是我的下一步。但我们不确定是否会得到同样的结果。我会尝试并发布结果。这是我的下一步。但我们不确定是否会得到同样的结果。我将尝试并发布结果。Content-Type:application/json;charset=utf-8Content-Type:application/json;charset=utf-8如何在$.getJSON方法上执行该操作?或者如何发送正确的内容类型?在最新的jQuery版本1.5.2中已经修复了它。如何在$.getJSON方法上发送?或者如何发送正确的内容类型?它已在最新的jQuery 1.5.2版中修复,并在..中报告了一个已确认的错误。在..中发现报告的已确认错误。