HTML:
ID:
User:
JavaScript:
var submit = document.getElementById("submit");
submit.onclick = function() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.state == 4) {
if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
console.log(xhr.responseText);
} else {
alert("HttpRequest was unsccessful: " + xhr.status);
}
}
}
xhr.open("post", "form.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var form = document.getElementById("info");
xhr.send(serialize(form));
}
form.php
$id = $_POST['id'];
$user = $_POST['user'];
echo $id . $user;
?>
代码如上, 我想要达到的效果, 是跟在form里添加了action和method属性一样, 提交后可以自动跳转到form.pp.
但是这样提交并没有反应, 搜了一下, 全是关于jquery的. 还是不知道用ajax提交表单是怎样的格式.