douluan5738 2014-04-09 19:06
浏览 43
已采纳

Javascript AJAX POST方法不将值发布到提交的页面

I am using Javascript AJAX to send request a php page and receiving the output from php page. When I use GET method in AJAX, its working. but the same is not working when i use POST method.

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

<?php
$mobile_number=trim($_POST['mobileNo']);
$sms_code=trim($_POST['code']);
$email_key=trim($_POST['emailKey']);
echo $mobile_number." - ".$sms_code." - ".$email_key;
?>
  • 写回答

1条回答 默认 最新

  • duanshan5259 2014-04-09 19:13
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站