IE不会触发jQuery Ajax的成功

我正在编写一个脚本,使用jQuery加载一些图像异步。

这是加载图像的函数的代码片段 –

try{ for(img in imgsArray){ $.ajax({ async: false, type: "get", url:imgsArray[img], success:function(imgFile){ alert("success"); //do something useful }, error:function(XMLHttpRequest,status,error){ //do nothing } });//ajax } } catch(e){ //oops } 

我已经在Firefox,Webkit(Safari,Chrome)中对此进行了测试,但它确实有效。

图像位于服务器上的文件夹中,我正在使用jQuery 1.3。

有任何想法吗?

解决此问题的一个简单方法是提供jQuery设置dataType : 'text'dataType : 'xml'dataType : 'json'或任何其他可用的响应类型。

我有同样的问题,但在.ajax调用中指定dataType设置后它工作正常。

IE实际上不是智能浏览器,它不承担默认值字符串。

试试吧……祝你好运。

尝试将cached-option设置为false。

  $.ajax({ async: false, cache: false, type: "get", url:imgsArray[img], success:function(imgFile){ alert("success"); //do something useful }, error:function(XMLHttpRequest,status,error){ //do nothing } });//ajax 

我遇到了类似的问题 – 如果IE无法将响应解析为xml,IE似乎会触发失败,即使请求成功,所以如果你请求图像,例如,它会返回xhr.status错误块中的200。

我在FF的成功块中以及在“if(xhr.status == 200)”条件中包含的错误块中保留了我的“成功”function。

使用以下标记来支持跨浏览器

$ .support.cors = true;

最后,我不得不为IE浏览器创建一个单独的function。

目标是在一个位置测试图像,以便代码看起来像 –

  //get the images from the array for(img in imgsArray){ if($.browser.msie){ /* DOM manipulation method */ //IE has problems with ajax so try this instead //create a new image object with unique ID var testImg = $(""); var imgID = "imgID"+img; //hide it.. testImg .css({ "visibility":"hidden" }); testImg .attr("src",imgsArray[img]); testImg .attr("id",imgID); //.. and insert it into the DOM or else test will always fail //because it does not exist as far as browser is concerned $("body").append(testImg ); //test for image if(document.getElementById(imgID).width > 60){ //do something useful } } 

这不是我的问题的答案,但它是一个function性的工作。

在过去的几天里,我和我的JSONP Web服务一直在使用IE和AJAX。 即使是代码中最小的错误也可能导致IE中的所有内容中断。

可能最好的办法是使用Visual Web Developer Express或Visual Studio在IE中调试页面。 这是一个如何做到这一点的教程:

如何使用Visual Web Developer Express调试JavaScript

按照说明操作,也可以在AJAX请求开始时设置断点。

尝试这些东西:

  • 从错误字段中删除“XMLHttpRequest”,我以前从未使用它,我的代码工作得很好;
  • 确保您使用的是最新版本的jquery(1.3.2)?

希望你能尽快开始工作!

设置’cache:false’里面.ajax配置对我有用:)

我建议使用Charles Proxy来查看发生了什么 – 即请求是否已经发出? 有什么回应?

此外,我认为可能会抛出一些exception,那么为什么不在Catch部分添加一些警报来查看会发生什么?

如果你成功的话:

 Success: function (data) { if (data != null) { alert(data); ; } }, 

为此改变:

  success: function (data, textStatus, XMLHttpRequest) { alert(data.d); } 

您可以尝试的另一件事,与John Saunders的解决方案非常相似,是尝试“jsonp”作为内容类型,而不是json – 用于json数据类型预期响应。 我不得不尝试用IE来让我的bug消失,这是除IE之外的其他浏览器中的普通代码。

这是我的代码:

 $.ajax({ url: request_url, data: { var1: 'stuff' }, dataType: "jsonp", type: "get", async: true, cache: false, success: function(collections) { // handle result } }); 

干杯。

我遇到了类似的问题。 以上答案解决了我的许多问题。 但是,我还需要做一件事。 不要用

 $.ajax({..., success: myfunc,...}); 

使用

 $.ajax({..., success: myfunc(),...}); 

如果您使用输入检查,如果您有一个选择为“已选择”,如果您不这样做,即不将此参数传递给ajax函数。