XMLHTTPRequest.send() in not working firefox

XMLHTTPRequest.send() in not working firefox

本文关键字:not working firefox in send XMLHTTPRequest      更新时间:2023-09-26

以下一段js代码不仅在Firefox中工作,而且在chrome和IE中运行良好。任何人都可以帮忙。

我正在尝试通过单击按钮提交两个表单

function abc(url){
var form = document.getElementById("sample");
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("actionCode=2");
var form1 = document.getElementById("sample1");
form1.submit();
}

奇怪的是,在 xhr.send() 行放置使用 firebug 的调试器时,它可以工作,一旦我从 firebug 中删除调试器,xhr.send 就不会执行。

任何建议都非常感谢。谢谢

这是异步请求,您需要检查请求的状态

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
 if (xhr.readyState == 4 && xhr.status == 200) {
   var form1 = document.getElementById("sample1");
   form1.submit();
 }
}
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type",  "application/x-www-form-urlencoded");
xhr.send("actionCode=2");
or this may help
xhr.open("POST", url, false); - it will make the request synchronous. 
Then you will not required to move submit or listen to events 

以及提交表单数据的其他方式https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects