gpt4 book ai didi

ajax - 即使我得到的响应为readyStatus 4和status 200,Jquery Ajax调用也会进入错误回调

转载 作者:行者123 更新时间:2023-12-01 01:47:05 76 4
gpt4 key购买 nike

我有一个使用 jQuery Ajax 调用的应用程序。它在 IE9、firefox、chrome 中运行良好。在IE 8中,即使响应为readyStatus 4和status 200,也会进入错误回调。

我在 IE8 的错误处理程序中得到以下响应

{"readyState":"4","status":"200","statusText":"success"}

我在其余浏览器(包括 IE9)的成功处理程序中得到以下响应

{
"Status": "Success",
"Roles": [
"Developer",
"RMG"
]
}

下面是我的ajax代码:

$.ajax({
cache : false,
type : "POST",
async : false,
url : server + "/Login",
dataType : "json",
data : requ,
contentType : "text/json; charset=UTF-8",
success : function(dr) {
data = dr;
console.log("Success "+JSON.stringify(data));
alert("Success "+JSON.stringify(data));
},
error : function(xhr) {
alert("Error "+JSON.stringify(xhr));
}
});

编辑:

添加了此脚本:

(function($){
alert("here0");
if (!$.support.cors && $.ajaxTransport && window.XDomainRequest) {
alert("here");
var httpRegEx = /^https?:\/\//i;
var getOrPostRegEx = /^get|post$/i;
var sameSchemeRegEx = new RegExp('^'+location.protocol, 'i');
var htmlRegEx = /text\/html/i;
var jsonRegEx = /\/json/i;
var xmlRegEx = /\/xml/i;

// ajaxTransport exists in jQuery 1.5+
$.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(options.url) && sameSchemeRegEx.test(options.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 === 'html' || htmlRegEx.test(xdr.contentType)) {
responses.html = xdr.responseText;
} else if (userType === 'json' || (userType !== 'text' && jsonRegEx.test(xdr.contentType))) {
try {
alert("JSON");
responses.json = $.parseJSON(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);
}
};
// set an empty handler for 'onprogress' so requests don't get aborted
xdr.onprogress = function(){};
xdr.onerror = function(){

complete(500, 'ERROR', {
text: xdr.responseText
});
};
var postData = '';
if (userOptions.data) {
postData = ($.type(userOptions.data) === 'string') ? userOptions.data : $.param(userOptions.data);
}
xdr.open(options.type, options.url);
xdr.send(postData);
},
abort: function(){
if (xdr) {
xdr.abort();
}
}
};
}
});
}

})(jQuery);

但仍然无法工作:(

最佳答案

删除“console.log(....)”

IE8 没有控制台,每次提到控制台都会抛出错误。

如果您想查看输出,请将其替换为alert(...)。 。 .

关于ajax - 即使我得到的响应为readyStatus 4和status 200,Jquery Ajax调用也会进入错误回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20501798/

76 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com