Javascript google chrome上的xmlhttprequest,有时返回net::connection\u重置错误
我的任务是创建一个到websocket服务器的xhr post请求 但是,无论何时使用chrome,它都会在50%的时间内返回net::ERR_CONNECTION_RESET 在safari、firefox和microsoft edge上测试的其他浏览器上不会出现这种情况Javascript google chrome上的xmlhttprequest,有时返回net::connection\u重置错误,javascript,ajax,google-chrome,xmlhttprequest,Javascript,Ajax,Google Chrome,Xmlhttprequest,我的任务是创建一个到websocket服务器的xhr post请求 但是,无论何时使用chrome,它都会在50%的时间内返回net::ERR_CONNECTION_RESET 在safari、firefox和microsoft edge上测试的其他浏览器上不会出现这种情况 function doPost(url, data) { promisePost(url, data) .then( function (val) { console.
function doPost(url, data) {
promisePost(url, data)
.then(
function (val) {
console.log("returns: " + val);
}, function (error) {
console.log("error: " + error);
})
};
var promisePost = function (url, data) {
return new Promise(function (resolve, reject) {
try {
var xhr = new XMLHttpRequest();
xhr.upload.onabort = function () {
reject("aborted by user");
};
xhr.timeout = 5000;
xhr.ontimeout = function () {
reject("error@timeout");
};
var handleStateChange = function () {
switch (xhr.readyState) {
case 4:
//responses
if (xhr.status == 0) {
//undefined error it return xhrstatus 0
reject("error@xhrcode:" + xhr.status);
} else {
if (xhr.responseText == "") {
reject("error@response is empty");
} else
resolve("done@xhrcode:" + xhr.status + "@responses:" + xhr.responseText);
}
break;
default:
//error with statuscode
reject("error@xhrcode:" + xhr.status + "@responses:" + xhr.responseText);
};
};
xhr.onerror = function (e) {
reject("status code: " + xhr.status + " " + e);
};
xhr.onreadystatechange = handleStateChange;
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.send(data);
} catch (e) {
//error in script
reject("scerror " + e.message);
}})
};
我100%确定服务器已经有cors支持,因为我已经通过其他浏览器对其进行了测试
有人知道怎么解决这个问题吗?在过去的3周里一直坚持这一点问题不在代码中,服务器端出了问题。检查。如果您的请求太“疯狂”,请检查您的服务器属性,可能它只是在超时时断开连接。问题是相同的代码在其他浏览器中工作正常,传输的数据量相同,net::connection\u reset错误似乎只存在于google chrome上。如果服务器越来越重,刚刚断开连接,我很确定其他浏览器上会显示错误或类似错误。您是从本地浏览器、远程PC还是通过HVPN进行测试?可能是软件错误?问题不在代码中,服务器端出了问题。检查。如果您的请求太“疯狂”,请检查您的服务器属性,可能它只是在超时时断开连接。问题是相同的代码在其他浏览器中工作正常,传输的数据量相同,net::connection\u reset错误似乎只存在于google chrome上。如果服务器越来越重,刚刚断开连接,我很确定其他浏览器上会显示错误或类似错误。您是从本地浏览器、远程PC还是通过HVPN进行测试?可能是软件错误?