【问题标题】:AJAX POST request on IE fails with error "No Transport"?IE 上的 AJAX POST 请求失败并出现错误“无传输”?
【发布时间】:2014-08-31 15:05:05
【问题描述】:

我正在尝试向公共服务发出 AJAX 请求

代码如下:

$.ajax({
    url : "http://api.geonames.org/citiesJSON",
    type : 'POST',
    cache : false,
    dataType : 'json',
    data : {
        username: "demo", 
        north:10, 
        south: 10, 
        east:10, 
        west:10}
}).done(function(data) {
    alert("Success: " + JSON.stringify(data));
}).fail(function(a, b, c, d) {
    alert("Failure: " 
          + JSON.stringify(a) + " " 
          + JSON.stringify(b) + " " 
          + JSON.stringify(c) + " " 
          + JSON.stringify(d) );
});

您可以在此链接中尝试:http://jsfiddle.net/hDXq3/

在 Chrome & Firefox 上成功获取响应,输出如下:

但对于 IE,失败警报:
失败:{"readyState":0,"status":0,"statusText":"No Transport"} "error" "No Transport" undefined



为什么它不能在 IE 上运行?以及如何解决这个问题?

【问题讨论】:

    标签: ajax json internet-explorer rest post


    【解决方案1】:

    有兴趣的可以参考一下解决方案:

    if (!jQuery.support.cors && window.XDomainRequest) {
        var httpRegEx = /^https?:\/\//i;
        var getOrPostRegEx = /^get|post$/i;
        var sameSchemeRegEx = new RegExp('^'+location.protocol, 'i');
        var xmlRegEx = /\/xml/i;
    
        // ajaxTransport exists in jQuery 1.5+
        jQuery.ajaxTransport('text html xml json', function(options, userOptions, jqXHR){
            // XDomainRequests must be: asynchronous, GET or POST methods, HTTP or HTTPS protocol, and same scheme as calling page
            if (options.crossDomain && options.async && getOrPostRegEx.test(options.type) && httpRegEx.test(userOptions.url) && sameSchemeRegEx.test(userOptions.url)) {
                var xdr = null;
                var userType = (userOptions.dataType||'').toLowerCase();
                return {
                    send: function(headers, complete){
                        xdr = new XDomainRequest();
                        if (/^\d+$/.test(userOptions.timeout)) {
                            xdr.timeout = userOptions.timeout;
                        }
                        xdr.ontimeout = function(){
                            complete(500, 'timeout');
                        };
                        xdr.onload = function(){
                            var allResponseHeaders = 'Content-Length: ' + xdr.responseText.length + '\r\nContent-Type: ' + xdr.contentType;
                            var status = {
                                code: 200,
                                message: 'success'
                            };
                            var responses = {
                                text: xdr.responseText
                            };
    
                                    try {
                                        if (userType === 'json') {
                                            try {
                                                responses.json = JSON.parse(xdr.responseText);
                                            } catch(e) {
                                                status.code = 500;
                                                status.message = 'parseerror';
                                                //throw 'Invalid JSON: ' + xdr.responseText;
                                            }
                                        } else if ((userType === 'xml') || ((userType !== 'text') && xmlRegEx.test(xdr.contentType))) {
                                            var doc = new ActiveXObject('Microsoft.XMLDOM');
                                            doc.async = false;
                                            try {
                                                doc.loadXML(xdr.responseText);
                                            } catch(e) {
                                                doc = undefined;
                                            }
                                            if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) {
                                                status.code = 500;
                                                status.message = 'parseerror';
                                                throw 'Invalid XML: ' + xdr.responseText;
                                            }
                                            responses.xml = doc;
                                        }
                                    } catch(parseMessage) {
                                        throw parseMessage;
                                    } finally {
                                        complete(status.code, status.message, responses, allResponseHeaders);
                                    }
                                };
                                xdr.onerror = function(){
                                    complete(500, 'error', {
                                        text: xdr.responseText
                                    });
                                };
                                xdr.open(options.type, options.url);
                                //xdr.send(userOptions.data);
                                xdr.send();
                            },
                            abort: function(){
                                if (xdr) {
                                    xdr.abort();
                                }
                            }
                        };
                    }
                });
        };
    
    jQuery.support.cors = true;
    
    $.ajax({
        url : "http://api.geonames.org/citiesJSON",
        crossDomain: true,
        type : 'POST',
        cache : false,
        dataType : 'json',
        data : {
            username: "demo", 
            north:10, 
            south: 10, 
            east:10, 
            west:10}
    }).done(function(data) {
        alert("Success: " + JSON.stringify(data));
    }).fail(function(a, b, c, d) {
        alert("Failure: " 
              + JSON.stringify(a) + " " 
              + JSON.stringify(b) + " " 
              + JSON.stringify(c) + " " 
              + JSON.stringify(d) );
    });
    

    您可以在此链接中尝试:http://jsfiddle.net/bjW8t/4/

    【讨论】:

    • 我在我的代码中尝试了您的示例,但出现 500 内部服务器错误。它在其他浏览器中工作得很好,但在 IE9 中却不行
    • 这是让我的 ie9 ajax 请求正常工作的唯一原因。添加 ajax 传输扩展 (Moonscript) 对我不起作用
    【解决方案2】:

    只需包含使用 XDomainRequest for IE8+ 的 jQuery ajaxTransport extension

    【讨论】:

    • 看起来很有趣。感谢分享。我试试看!
    猜你喜欢
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 2020-12-28
    • 2014-04-07
    • 2010-11-22
    • 2011-11-08
    相关资源
    最近更新 更多