无法在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个回答



$ _ POST </ code>处理内容类型:application / x-www-form-urlencoded </ code> not JSON to get JSON use </ p>

  $ data = json_decode(file_get_contents(“php:// input”)); 
</ code> </ pre>

</ div>

展开原文

原文

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

$data = json_decode(file_get_contents("php://input"));

douxiza9868
douxiza9868 谢谢。非常清楚。
7 年多之前 回复
douwen1313
douwen1313 似乎不起作用。 jsfiddle.net/2TpTk
7 年多之前 回复
drze7794
drze7794 谢谢@Musa它奏效了。 但我有一个疑问,对于另一个页面代码是如何工作的:代码是:function showUser(){var email =“swati@gmail.com”; var pwd =“swati”; $ .ajax({type:“POST”,url:'ajatus.in/ajatus_templates/Templaton/wp-content/plugins /...',dataType:'json',async:false,// json对象发送到身份验证 url data:JSON.stringify({“email”:email,“pwd”:pwd}),success:function(data){alert(data);}}); }
7 年多之前 回复
dongmale0656
dongmale0656 $ data = json_decode之后(file_get_contents(“php:// input”)); 只需使用$ data而不是$ _POST。
7 年多之前 回复
douyan4470
douyan4470 但是,相同的代码适用于另一个用于登录的页面。 你能解释一下我可以把代码放在哪里$ data = json_decode(file_get_contents(“php:// input”)); 使它工作。 我需要在PHP文件中接收数据并进行注册并且还可以访问服务器。
7 年多之前 回复
立即提问