jQuery的阿贾克斯,没有工作在Internet Explorer [英] Jquery Ajax, not working in Internet explorer
问题描述
我试图做一些jQuery的Ajax和它在Firfox和Chrome,但不是在Internet Explorer 9中。
I'm trying to do some jQuery ajax and it works in Firfox and Chrome, but not in internet explorer 9.
最后code将不得不跨子域,而这并不在IE浏览器的默认运输工作。
The final code will have to go across sub-domains, and this doesn't work in ie with the default transport.
所以我想创建一个自定义运输在Internet Explorer中使用
So I'm trying to create a custom transport to use in internet explorer
方法1
$.ajaxTransport("+*", function (options, originalOptions, jqXHR) {
if (jQuery.browser.msie && window.XDomainRequest) {
var xdr;
return {
send: function (headers, completeCallback) {
// Use Microsoft XDR
xdr = new XDomainRequest();
xdr.open("get", options.url);
xdr.onload = function () {
if (this.contentType.match(/\/xml/)) {
var dom = new ActiveXObject("Microsoft.XMLDOM");
dom.async = false;
dom.loadXML(this.responseText);
completeCallback(200, "success", [dom]);
} else {
completeCallback(200, "success", [this.responseText]);
}
};
xdr.ontimeout = function () {
completeCallback(408, "error", ["The request timed out."]);
};
xdr.onerror = function () {
completeCallback(404, "error", ["The requested resource could not be found."]);
};
xdr.send();
},
abort: function () {
if (xdr) xdr.abort();
}
};
}
});
我创建了一个简单的示例页面,在这里展示了第一种方法: http://services.whygo.net/sendAjax.htm
I've created a simple sample page to demonstrate the first technique here: http://services.whygo.net/sendAjax.htm
请注意,如果您使用自定义的交通工具,那么正常的运输将失败,除非你刷新
这个想法来自这里: http://forum.jquery.com/topic/cross-domain -ajax和 - 即#14737000002203097
The idea comes from here: http://forum.jquery.com/topic/cross-domain-ajax-and-ie#14737000002203097
这给叫上$ AJAX的'错误'的方法里面不是错误等错误消息,当它失败。我得到一个405的方法,如果开发工具网络选项卡上不允许的,但是服务器端的东西,确实执行。
This give no error message other than 'error' inside the 'error' method called on $ajax, when it fails. I do get a 405 Method not allowed on the 'Network' tab of if dev tools, but the server side stuff does execute.
方法2 我也曾尝试另一种方法如下所述: <一href="http://stackoverflow.com/questions/11143226/cross-subdomain-ajax-works-in-chrome-not-ie">Cross-subdomain Ajax的工作原理在Chrome,IE不
Method 2 I have also tried another method as described here: Cross-subdomain AJAX works in Chrome, not IE
if ('XDomainRequest' in window && window.XDomainRequest !== null) {
// override default jQuery transport
jQuery.ajaxSettings.xhr = function() {
try { return new XDomainRequest(); }
catch(e) { }
};
}
这可以在这里找到: http://www.whygo.net/sendAjax2.html
This can be found here: http://www.whygo.net/sendAjax2.html
在这一个我真正得到200 codeS的就是开发工具的网络选项卡上,但不叫'错误'或$ AJAX的'成功'pararm。
On this one I actually get 200 codes on the 'network' tab of ie dev tools, but doesn't call the 'error' or the 'success' pararm of $ajax.
如果我把超时在这第二个,然后将其返回到错误功能与超时的消息。
If I put a timeout on this second one, then it returns to the 'error' function with a message of 'timeout'.
推荐答案
下面是我用这种不一致...
Here's the solution I went with after about a day of struggling with this inconsistency...
// new method as to not overwrite jQuery's defaults
var cors = (window.XDomainRequest) ? function(url, callback) {
var xdr = new XDomainRequest();
xdr.open('get', url);
xdr.onload = function() { callback(xdr.responseText); }
xdr.send();
} : $.get; // else, use jQuery's method
使用...
cors(url, function(msg) { alert(msg); }); // pretty well same as $.get
复制和粘贴,这当然不符合所有的目的,但它是一个开始,它的工作原理。
Copy and paste, this of course doesn't serve all purposes, but it's a start and it works.
这篇关于jQuery的阿贾克斯,没有工作在Internet Explorer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!