問題描述
我知道這有點自相矛盾,因為 XMLHttpRequest 不應該重新加載頁面,這就是它的全部意義所在.
I know this is a bit paradoxal because XMLHttpRequest shouldn't be reloading the page, and it's the whole point of it.
在 Chrome 最新版本、iOS 上的 Safari 和 Android 上進行了嘗試.結果都一樣.
Tried in Chrome latest version, Safari on iOS and on an Android. All same result.
我正在通過它發送一個帶有文件的表格.效果很好,目標站點正確接收數據并顯示它.發回 200,OK".(這是臉書)
I am sending a form through it, with files. Works great, the destination site receive correctly the data, and displays it. Sends back a 200, "OK". (It's facebook)
但是我的頁面會自動重新加載.就像我使用 HTML 表單和提交按鈕提交表單一樣.(這是我原來的問題)
But then my page reloads automatically. Just like if I submitted the form using HTML form and a submit button. (Which was my original problem)
這是我的做法,來自 Javascript
Here is how I do it, from Javascript
// Get the form element
var formData = new FormData(document.getElementById("photosubmitform"));
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://graph.facebook.com/' + facebookWallId + '/photos', false);
xhr.onload = function(event)
{
var json = xhr.responseText; // Response, yay!
}
xhr.send(formData); // Sending it, will reload the page on success...
推薦答案
您是否有可能通過提交表單來觸發此問題?如果您不在 onsubmit 處理程序中return false
,表單仍然會被提交.
Any chance you're triggering this by submitting a form? If you don't return false
in the onsubmit handler, the form will still be submitted.
這篇關于如何避免在 XMLHttpRequest 調用后自動重新加載頁面?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!