在 Chrome & 上成功检索到响应Firefox,输出如下:但是对于 IE,失败警报: 失败:{"readyState":0,"status":0,"statusText":"No Transport"} "error" "No Transport" undefined 为什么它不能在 IE 上运行?以及如何解决这个问题? 解决方案 以下是给有兴趣的人的解决方案: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 存在于 jQuery 1.5+jQuery.ajaxTransport('text html xml json', function(options, userOptions, jqXHR){//XDomainRequests 必须是:异步,GET 或 POST 方法,HTTP 或 HTTPS 协议,与调用页面的方案相同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();返回 {发送:功能(标题,完成){xdr = 新的 XDomainRequest();如果 (/^d+$/.test(userOptions.timeout)) {xdr.timeout = userOptions.timeout;}xdr.ontimeout = 函数(){完成(500,'超时');};xdr.onload = 函数(){var allResponseHeaders = '内容长度:' + xdr.responseText.length + '内容类型:' + xdr.contentType;变量状态 = {代码:200,消息:'成功'};变量响应 = {文本:xdr.responseText};尝试 {如果(用户类型 === 'json'){尝试 {响应.json = JSON.parse(xdr.responseText);}赶上(e){状态代码 = 500;status.message = '解析错误';//抛出'无效的JSON:' + xdr.responseText;}} else if ((userType === 'xml') || ((userType !== 'text') && xmlRegEx.test(xdr.contentType))) {var doc = new ActiveXObject('Microsoft.XMLDOM');doc.async = 假;尝试 {doc.loadXML(xdr.responseText);}赶上(e){文档 = 未定义;}if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) {状态代码 = 500;status.message = '解析错误';抛出 '无效的 XML:' + xdr.responseText;}响应.xml = 文档;}} 捕捉(解析消息){抛出 parseMessage;} 最后 {完成(状态.代码,状态.消息,响应,allResponseHeaders);}};xdr.onerror = 函数(){完成(500,'错误',{文本:xdr.responseText});};xdr.open(options.type, options.url);//xdr.send(userOptions.data);xdr.send();},中止:函数(){如果(xdr){xdr.abort();}}};}});};jQuery.support.cors = true;$.ajax({url : "http://api.geonames.org/citiesJSON",跨域:真,类型:'POST',缓存:假,数据类型:'json',数据 : {用户名:演示",北:10,南:10,东:10,西:10}}).完成(功能(数据){alert("成功:" + JSON.stringify(data));}).失败(函数(a,b,c,d){alert("失败:"+ JSON.stringify(a) + " "+ JSON.stringify(b) + " "+ JSON.stringify(c) + " "+ JSON.stringify(d));});您可以在此链接中尝试:http://jsfiddle.net/bjW8t/4/I'm trying to make an AJAX request to a public serviceHere's the 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) );});You may try it in this link: http://jsfiddle.net/hDXq3/The response is retrieved successfully on Chrome & Firefox, and the output is as follows:But for IE, the fails alerting: Failure: {"readyState":0,"status":0,"statusText":"No Transport"} "error" "No Transport" undefined 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 + 'Content-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) );});You may try it in this link: http://jsfiddle.net/bjW8t/4/ 这篇关于IE 上的 AJAX POST 请求失败并显示错误“无传输"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 23:09