XMLHttpRequest POST and Firefox



我有一个带有javascript的网页,可以将json数据发布到python flask应用程序中。Chrome、Edge、Opera、Android、a.s.o的一切都很好。只是Firefox给了我一个错误。

这是我的javascript:

const xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.setRequestHeader("Authorization", authdata);
xhr.onreadystatechange = function(ev) {
//2 - request sent, 3 - something back, 4 - full response
//console.log(xhr.readyState);
if (xhr.readyState === 4) {
switch (xhr.status) {
case 200:
case 304:
console.log("OK or Not Modified (cached)", xhr.status);
console.log(xhr.responseText);
break;
case 201:
console.log("Created", xhr.status);
console.log(xhr.responseText);
break;
case 400:
console.log("Bad Request", xhr.status);
alert("Bad Request");
break;
case 401:
case 403:
console.log("Not Authorized or Forbidden", xhr.status);
alert("Not Authorized or Forbidden");
break;
case 404:
console.log("Not Found", xhr.status);
alert("404 Not Found");
break;
case 500:
console.log("Server Side Error", xhr.status);
alert("Server Error 01 Code: " + xhr.status.toString());
break;
default:
console.log("Some other code: ", xhr.status);
alert("Server Error 02 Code: " + xhr.status.toString());
}
}
};
xhr.onerror = function(err) {
console.warn(err);
alert("Server Error 99", err);
};
edata = JSON.stringify({ "domain": "workdomain", "zonedata": "data" });
xhr.send(edata);

当我在Firefox中启动它时;服务器错误02";状态代码0若我查看调试器网络选项卡,并没有POST发送。

这是Firefox的问题吗?

玩了一段时间后,我发现这是Firefox中的一个时间问题。

我在send语句中添加了一个setTimeout,一切都运行得很顺利。

以下是现在有效的代码:

const xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.timeout = 0;
xhr.setRequestHeader("Authorization", authdata);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "*/*");
setTimeout(function() {
xhr.send(edata);
}, 1000);
xhr.onerror = function(err) {
console.warn(err);
alert("Server Error no response", err);
};

最新更新