duanlu8613 2013-02-15 08:05
浏览 55
已采纳

无法在PHP中访问jquery Json POST参数,但使用Ajax和Normal帖子

When i Send POST parameters Through AJax i get a response back as required in the following url but not through Json. I have even done a print_r($_POST) to check at the server and get a blank array in Json. Can you tell me what i am doing wrong. In other pages my Json are working fine the same way.

url: http://www.ajatus.in/ajatus_templates/Templaton/wp-content/plugins/realestate/registrationJson.php


Ajax Code:

    <html>
    <head>
    <script language="javascript" type="text/javascript">

    function showUser()
    {

    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)
        {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
       var title="My title";
      var fname="Asish";
      var lname="Mohanty";
      var city="BBSR";
      var conNo="9475654563";
      var pwd="asish";
      var email="asish@gmail.com";
      var state="Odisha";
      var country="India";


     xmlhttp.open("POST","http://www.ajatus.in/ajatus_templates/Templaton/wp-content/plugins/realestate/registrationJson.php",true);
     xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
     xmlhttp.send("title="+title+"&fname="+fname+"&lname="+lname+"&city="+city+"&conNo="+conNo+"&pwd="+pwd+"&email="+email+"&state="+state+"&country="+country);

    }
    </script>
    </head>
    <body>

    <form method="post">
    <input type="button" value="SUBMIT" onClick="showUser()">

    </form>
    <br />
    <div id="txtHint" align="center"><b></b></div>

    </body>
    </html>

Json code:

    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script language="javascript" type="text/javascript">
    function showUser() {
      var title="My title";
      var fname="Asish";
      var lname="Mohanty";
      var city="BBSR";
      var conNo="9475654563";
      var pwd="asish";
      var email="asish@gmail.com";
      var state="Odisha";
      var country="India";
        $.ajax({
            type: "POST",
            //the url where you want to sent the userName and password to
            url: 'http://www.ajatus.in/ajatus_templates/Templaton/wp-content/plugins/realestate/registrationJson.php',
            dataType: 'json',
            async: false,
            //json object to sent to the authentication url
            data: JSON.stringify({ "title" : title,"fname" : fname,"lname": lname,"city" : city,"conNo": conNo,"pwd" : pwd,"email": email,"state" : state,"country" : country }),
            success: function (data) {

            alert(data); 
            }
        });
    }
    </script>
    </head>
    <body>

    <form method="post">
    <input type="button" value="SUBMIT" onClick="showUser()">

    </form>

    </body>
    </html>
  • 写回答

1条回答 默认 最新

  • dongzanxun2790 2013-02-15 08:13
    关注

    $_POST handles Content-type:application/x-www-form-urlencoded not JSON to get the JSON use

    $data = json_decode(file_get_contents("php://input"));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料