IE 不触发 jQuery Ajax 成功
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/782532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IE not triggering jQuery Ajax success
提问by Peter
I'm working on a script to load some images async using jQuery.
我正在编写一个脚本来使用 jQuery 异步加载一些图像。
Here is a code snippet of the function that loads the images -
这是加载图像的函数的代码片段 -
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
}
I have tested this in Firefox, Webkit (Safari,Chrome) and it works.
我已经在 Firefox、Webkit(Safari、Chrome)中对此进行了测试,并且可以正常工作。
The images are in a folder on the server and I'm using jQuery 1.3.
图像位于服务器上的文件夹中,我使用的是 jQuery 1.3。
any ideas?
有任何想法吗?
回答by Amber Mahajan
A simple fix of this problem is to provide the jQuery setting dataType : 'text'
or dataType : 'xml'
or dataType : 'json'
or any other available response type.
解决此问题的一个简单方法是提供 jQuery 设置dataType : 'text'
或dataType : 'xml'
或dataType : 'json'
或任何其他可用的响应类型。
I had same problem, but it's working fine after specifying the dataType setting in the .ajax call.
我遇到了同样的问题,但在 .ajax 调用中指定 dataType 设置后它工作正常。
IE is really not an intelligent browser, it doesn't assume the default value string.
IE 真的不是智能浏览器,它不假定默认值字符串。
Try it... good luck.
试试吧……祝你好运。
回答by John Korsnes
Try setting the cached-option to false.
尝试将缓存选项设置为 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
回答by John Korsnes
I had a similar problem -- IE appears to trigger failure if it can't parse the response as xml, even if the request was a success, so if you're requesting an image, for example, it would return a xhr.status of 200 in the error block.
我有一个类似的问题 - 如果 IE 无法将响应解析为 xml,则它似乎会触发失败,即使请求成功,例如,如果您请求图像,它会返回 xhr.status错误块中的 200 个。
I stuck my "success" functionality in the success block for FF and in the error block wrapped in an "if (xhr.status == 200)" conditional.
我在 FF 的成功块和包裹在“if (xhr.status == 200)”条件中的错误块中卡住了我的“成功”功能。
回答by cherry
use the following tag to support cross browser
使用以下标签支持跨浏览器
$.support.cors = true;
$.support.cors = true;
回答by Aviad
set 'cache: false' inside .ajax config works for me :)
在 .ajax 配置中设置 'cache: false' 对我有用:)
回答by Simeon Wislang
I've been having similar problems with IE and AJAX over the past few days with my JSONP Web Service. Even the smallest error in your code can cause everything to break in IE.
在过去的几天里,我的 JSONP Web 服务在 IE 和 AJAX 方面遇到了类似的问题。即使代码中最小的错误也可能导致 IE 中的所有内容中断。
Probably the best thing to do is debug the page in IE using Visual Web Developer Express or Visual Studio. Here is a tutorial of how to do it:
可能最好的办法是使用 Visual Web Developer Express 或 Visual Studio 在 IE 中调试页面。这是如何做到这一点的教程:
How to debug JavaScript with Visual Web Developer Express
如何使用 Visual Web Developer Express 调试 JavaScript
Follow the instructions and maybe place breakpoints on at the begining of the AJAX request.
按照说明操作,并可能在 AJAX 请求的开头放置断点。
Try these things too:
也试试这些东西:
- Removing "XMLHttpRequest" from the error field, I have never used it before and my code works just fine;
- Make sure you are using the latest version of jquery (1.3.2)?
- 从错误字段中删除“XMLHttpRequest”,我以前从未使用过它,我的代码运行良好;
- 确保您使用的是最新版本的 jquery (1.3.2)?
Hope you get it working soon!
希望你能尽快让它工作!
回答by Peter
In the end I had to create a separate function for IE browsers.
最后我不得不为 IE 浏览器创建一个单独的函数。
The objective was to test for an image at a location so the code looked something like -
目标是在某个位置测试图像,因此代码看起来像-
//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 = $("<img/>");
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
}
}
It is not really the answer to my question but it's a functional work around.
这并不是我问题的真正答案,但它是一种功能性的解决方法。
回答by Seb
I'd suggest using Charles Proxyto see what's going on - i.e. is the request going out at all? What's the response?
我建议使用Charles Proxy来查看发生了什么 - 即请求是否已经发出?有什么反应?
Also, I think there might be some Exception thrown, so why don't you add some alerts in the Catch section to see what happens?
另外,我认为可能会抛出一些异常,那么为什么不在 Catch 部分添加一些警报以查看会发生什么?
回答by user1809556
if you success is:
如果你的成功是:
Success: function (data) {
if (data != null) {
alert(data);
;
}
},
change for this:
为此更改:
success: function (data, textStatus, XMLHttpRequest) {
alert(data.d);
}
回答by uch
Another thing you can try, very much the same as the solution from John Saunders, is try "jsonp" as the content type, instead of json - for a json data type expected response. I had to try this with IE to get my bug to go away, which was the normal code working in every other browser, except IE.
您可以尝试的另一件事,与 John Saunders 的解决方案非常相似,是尝试将“jsonp”作为内容类型,而不是 json - 对于 json 数据类型的预期响应。我不得不在 IE 上尝试这个才能让我的错误消失,这是在除 IE 之外的所有其他浏览器中工作的正常代码。
Here's my code that worked:
这是我的工作代码:
$.ajax({
url: request_url,
data: { var1: 'stuff' },
dataType: "jsonp",
type: "get",
async: true,
cache: false,
success: function(collections) {
// handle result
}
});
Cheers.
干杯。