Javascript 如何在没有jQuery的情况下使用ajax Post方法提交用户输入
我正在尝试制作一个订阅表单,该表单使用ajaxpost方法提交表单,而不是jQuery。我写了一个代码,但它不能正常工作。我认为我在发送POST数据方面做错了。这是我的代码,到目前为止我已经尝试过了Javascript 如何在没有jQuery的情况下使用ajax Post方法提交用户输入,javascript,php,html,ajax,post,Javascript,Php,Html,Ajax,Post,我正在尝试制作一个订阅表单,该表单使用ajaxpost方法提交表单,而不是jQuery。我写了一个代码,但它不能正常工作。我认为我在发送POST数据方面做错了。这是我的代码,到目前为止我已经尝试过了 <div id="form" style="max-width:477px"> <div class="imgContainer" id="myImageContainer"> <span onclick="document.getElementByI
<div id="form" style="max-width:477px">
<div class="imgContainer" id="myImageContainer">
<span onclick="document.getElementById('myLogin').style.display='none'" class="close" title="Close">×</span>
</div>
<div class="loginInfo" id="myLoginInfo">
<label for="Full Name">
<b>Full Name</b>
</label>
<spam class="error" style="color:red">
<i id="nameErr"></i>
</spam>
<input type="text" placeholder="Enter full name" name="name" id="name">
<label for="Email"><b>Email</b></label>
<spam class="error" style="color:red">
<i id="emailErr"> </i>
</spam>
<input type="text" placeholder="Enter your Email" name="email" id="email">
<button onclick="verify()">Subscribe</button>
</div>
</div>
</div>
<script>
function verify(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
document.getElementById("email").innerHTML = myObj.email;
document.getElementById("emailErr").innerHTML = myObj.emailErr;
document.getElementById("name").innerHTML = myObj.name;
document.getElementById("nameErr").innerHTML = myObj.nameErr;
}
};
xmlhttp.open("GET", "subs.php", true);
var data="email=" + document.getElementById("email").value + "&name="+document.getElementById("name").value;
xmlhttp.send(data);
}
</script>
当我点击subscribe按钮时,它给我空值,而不是显示错误消息
xmlhttp.open("GET", "subs.php", true);
应该是
xmlhttp.open("POST", "subs.php", true);
如果PHP代码需要POST请求。建议您使用比Jquery和纯javascript更清晰、更简单的方法发送ajax从哪里获取空值使用“get”方法发送,但通过“POST”方法接收
xmlhttp.open("POST", "subs.php", true);