1

当我通过 AJax 发送 POST 参数时,我会根据以下 url 的要求得到响应,但不是通过 Json。我什至做了一个 print_r($_POST) 来检查服务器并在 Json 中获取一个空白数组。你能告诉我我做错了什么吗?在其他页面中,我的 Json 以同样的方式工作正常。

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


阿贾克斯代码:

    <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代码:

    <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>
4

1 回答 1

2

$_POST处理Content-type:application/x-www-form-urlencoded非 JSON 以获取 JSON 使用

$data = json_decode(file_get_contents("php://input"));
于 2013-02-15T08:13:02.370 回答