[英]XMLHTTPRequest.send() in not working firefox
以下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()行時,它起作用了,一旦我從調試器中刪除了調試器,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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.