Javascript 通过ajax发送POST请求时出错
我想发送一个post请求,并从php文件中获取一个回音。但我找不到我的错误,总是得到一个通知:未定义的索引 请不要用jqueryJavascript 通过ajax发送POST请求时出错,javascript,ajax,http-post,Javascript,Ajax,Http Post,我想发送一个post请求,并从php文件中获取一个回音。但我找不到我的错误,总是得到一个通知:未定义的索引 请不要用jquery <form id="register" method="POST" action="register-action.php"> <input type="text" name="first_name" placeholder="Onoma"><br> <input type="text" name="last
<form id="register" method="POST" action="register-action.php">
<input type="text" name="first_name" placeholder="Onoma"><br>
<input type="text" name="last_name" placeholder="Epitheto"><br>
<input type="text" name="mail" placeholder="E-mail"><br>
<input type="password" name="pass" placeholder="password"><br>
<select name="eidikotita_id">
<?php while($eid = $eidikotites->fetch( PDO::FETCH_ASSOC )):
echo "<option value={$eid['id_eidikotitas']}>{$eid['titlos']}</option>";
endwhile; ?>
</select>
<input type="submit" value="Register"><br>
</form>
/////////////////////////////////////////////////////////////////////////////
var mail_input = document.querySelector("#registerinput[name='mail']").value;
alert(mail_input);
document.querySelector("#register input[name='first_name']").focus();
document.querySelector("#register input[name='mail']").onblur = function() {
var request = new XMLHttpRequest();//instantiate an XMLHttpRequest object
request.open("POST", "register-action.php", true);//Specifying the Request
request.setRequestHeader("Content-Type", "text/plain");//POST requests,for example, need a “Content-Type” header
request.send(mail_input);//specify the optional request body. GET requests never have a body, so you should pass null or omit the argument.
};
<?php
echo $_POST['mail'];
?>
只需print\u r()
发布数据即可查看问题所在
xhr.send()
应该是这样的:xhr.send('mail='+encodeURIComponent(email))
,因为您想要$\u POST['mail']
。您的选择器也有问题,应该是“#注册输入[name='mail']”
document.querySelector("#register input[name='first_name']").focus();
document.querySelector("#register input[name='mail']").onblur = function() {
var mail_input = document.querySelector("#register input[name='mail']").value;
var request = new XMLHttpRequest();//instantiate an XMLHttpRequest object
request.open("GET", "register-action.php?mail="+mail_input, true);//Specifying the Request
request.setRequestHeader("Content-Type", "text/plain");//POST requests,for example, need a “Content-Type” header
request.send(encodeURIComponent(mail_input));//specify the optional request body. GET requests never have a body, so you should pass null or omit the argument.
//////send succcess
if (request.readyState === 4 && request.status === 200) {
request.onreadystatechange = function(){
console.log(request.responseText);
}
}
};