Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
905 views
in Technique[技术] by (71.8m points)

asp.net - Jquery Ajax, not working in Internet explorer

I'm trying to do some jQuery ajax and it works in Firfox and Chrome, but not in internet explorer 9.

The final code will have to go across sub-domains, and this doesn't work in ie with the default transport.

So I'm trying to create a custom transport to use in internet explorer

Method 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();
            }
        };
    }
});

I've created a simple sample page to demonstrate the first technique here: http://services.whygo.net/sendAjax.htm

Please note if you use the custom transport the normal transport will then fail unless you refresh

The idea comes from here: http://forum.jquery.com/topic/cross-domain-ajax-and-ie#14737000002203097

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.

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) { }
     };
}

This can be found here: http://www.whygo.net/sendAjax2.html

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'.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Use...

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...