在IE浏览器的AJAX POST请求失败,错误[否运输"? [英] AJAX POST request on IE fails with error "No Transport"?

查看:443
本文介绍了在IE浏览器的AJAX POST请求失败,错误[否运输"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个AJAX请求到一个公共服务

I'm trying to make an AJAX request to a public service

这里的code:

$.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/

You may try it in this link: http://jsfiddle.net/hDXq3/

响应成功检索的Chrome浏览器和Firefox和输出如下:

The response is retrieved successfully on Chrome & Firefox, and the output is as follows:

不过对于IE浏览器,在出现故障报警:
失败:{readyState的0状态:0,状态文本:没有交通}错误,没有交通未定义

But for IE, the fails alerting:
Failure: {"readyState":0,"status":0,"statusText":"No Transport"} "error" "No Transport" undefined



为什么它不工作的IE浏览器?如何解决这一问题?



Why it's not working on IE ? and how to fix this ?

推荐答案

这里的解决方案,对于那些谁感兴趣的是:

Here's the solution for those who are interested:

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/

You may try it in this link: http://jsfiddle.net/bjW8t/4/

这篇关于在IE浏览器的AJAX POST请求失败,错误[否运输"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆