[英]Javascript AJAX POST Method not posting values to the submitted page
I am using Javascript AJAX to send request a php page and receiving the output from php page. 我正在使用Javascript AJAX发送请求的php页面并接收来自php页面的输出。 When I use
GET
method in AJAX, its working. 当我在AJAX中使用
GET
方法时,它可以工作。 but the same is not working when i use POST
method. 但是当我使用
POST
方法时,这是行不通的。
here is my code: 这是我的代码:
<script>
function verifyMobile(mobileNo,code,emailKey)
{
alert(mobileNo+'-'+code+'-'+emailKey); //alerts proper value here...
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText); //alerts empty value here...
}
xmlhttp.open("POST","verification.php",true);
xmlhttp.send("mobileNo="+mobileNo+"&code="+code+"&emailKey="+emailKey);
}
</script>
verification.php verification.php
<?php
$mobile_number=trim($_POST['mobileNo']);
$sms_code=trim($_POST['code']);
$email_key=trim($_POST['emailKey']);
echo $mobile_number." - ".$sms_code." - ".$email_key;
?>
Try setting the headers and see if it helps. 尝试设置标题,看看是否有帮助。
var params = "mobileNo="+encodeURIComponent(mobileNo)+"&code="+encodeURIComponent(code)+"&emailKey="+encodeURIComponent(emailKey);
xmlhttp.open("POST","verification.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.