1 回答

TA貢獻(xiàn)1796條經(jīng)驗 獲得超10個贊
是的,絕對是。您幾乎自己回答了這個問題。這里有一些代碼可以幫助您入門。
document.querySelector(".whateverFormSubmit").click(function(e){
e.preventDefault();
let formInfo = document.querySelector("input1");
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.querySelector("response").innerHTML = this.responseText;
}
};
xhttp.open("POST", "ajax.php", true);
xhttp.send('formInfo=' + formInfo);
})
請注意 preventDefault(),如果您不使用它,您的表單將通過 HTTP 正文發(fā)布,從而使您的 AJAX 無效。
就這樣,您在不刷新頁面的情況下將信息從前端發(fā)布到后端。當(dāng)然,您可以隨意發(fā)送 JSON,但我盡量保持簡單。
- 1 回答
- 0 關(guān)注
- 116 瀏覽
添加回答
舉報