为什么这个简单的jQuery getJSON不能在IE8中运行?

我有一个非常标准的AJAX请求:

$.getJSON('/products/findmatching/38647.json', {}, function(JsonData){ var tableHtml = ''; var x; for (x in JsonData.matchingProds) { var matchingProd = JsonData.matchingProds[x]; var buyMessage; if ( x == 0 ) { buyMessage = 'Buy Cheapest'; } else { buyMessage = 'Buy from this shop'; } tableHtml = tableHtml + '' + matchingProd.name + ' \ ' + matchingProd.name + ' \ ' + matchingProd._merchant.title + ' \ £' + matchingProd.price + ' \ ' + buyMessage + ''; } $('#matchingproducts tbody').html(tableHtml); $('#loading').delay(1000).fadeOut(); }); 

它在除IE之外的所有浏览器中都能正常工 因为我有Mac,所以我在IE中做的不多,但我在XP虚拟机上安装了IE8。 使用其内置的调试工具并没有真正帮助(它们不是很好)。 我唯一能理解的是,在某些时候我得到了“预期标识符”错误。

这可能是在返回的JSON数据中吗? 如何从IE的角度检查数据?

好吧我明白了。 有人建议尝试一个非缩小版的jQu​​ery。 我做了这个并逐步完成了IE8s的Javascript调试器。 在某个时刻,出现以下错误:

 Could not complete the operation due to error c00ce56e. 

一个小小的谷歌搜索发现它是我为我的JSON数据设置的字符集声明。 在PHP中,这是通过以下方式完成的:

 header ( 'Content-Type: text/javascript; charset=utf8' ); 

事实certificateIE是非常特别的charset参考( http://forums.asp.net/t/1345268.aspx#2732852 ),所以我把它改为:

 header ( 'Content-Type: text/javascript; charset=UTF-8' ); 

嘿-presto,它就像一个魅力。 谢谢你的帮助,你再次指出了我正确的方向!

再次编辑 – 仍在调试 – 更改为使用其他函数需要将最后一个参数设为myAjaxResponderFunc ,不带引号…

 $.ajaxSetup({ cache: false }); 

您必须使用IE8 +的检查浏览器和版本,然后使用XDomainRequest(),如果msie8 +。

这将返回一个JSON字符串,必须使用jQuery.parseJSON()来创建JSON对象…

不要使用getJSON!

这是我的例子:

 if ($.browser.msie && parseInt($.browser.version, 10) >= 8 && window.XDomainRequest) { // Use Microsoft XDR var xdr = new XDomainRequest(); xdr.open("get", reqURL); xdr.onload = function() { var json = xdr.responseText; json = $.parseJSON(json); $.each(json.results, function(i, val) { console.log(val.formatted_address); var locString = val.formatted_address; $.each(val.address_components, function(x, comp) { if($.inArray("postal_code", comp.types) > -1) { //alert("___" + comp.types); zipmap[locString] = comp.short_name; } }); suggestions.push(val.formatted_address); }); //alert(json.results); } xdr.send(); add(suggestions); }else { $.getJSON(reqURL, function(data) { var isZIP = new Boolean; console.log(data.results); $.each(data.results, function(i, val) { console.log(val.formatted_address); var locString = val.formatted_address; $.each(val.address_components, function(x, comp) { if($.inArray("postal_code", comp.types) > -1) { console.log("___" + comp.types); zipmap[locString] = comp.short_name; } }); suggestions.push(val.formatted_address); }); add(suggestions); }); } 

requrl是您发出请求的URL。

完成!

相信: http : //graphicmaniacs.com/note/getting-a-cross-domain-json-with-jquery-in-internet-explorer-8-and-later/

我只喜欢IE!

嗯……看来你的脚本在IE中运行正常。 唯一看起来破坏的是你的jQuery fadeOut方法。 我能在这里找到一些相关信息:

jquery IE Fadein和Fadeout Opacity

基本上,IE在以前没有声明过时会出现改变CSS属性的问题。

编辑:也许它在IE中运行不正常…我可能没有理解页面加载的确切过程。