无法在PHP中访问jquery Json POST参数,但使用Ajax和Normal post [英] Cannot access jquery Json POST parameters in PHP but working with Ajax and Normal post

查看:80
本文介绍了无法在PHP中访问jquery Json POST参数,但使用Ajax和Normal post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过AJax发送POST参数时,按照以下URL的要求返回了响应,但没有通过Json返回.我什至完成了print_r($ _ POST)来检查服务器并在Json中获得一个空白数组.你能告诉我我做错了吗.在其他页面中,我的Json可以按相同方式正常工作.

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.在/ajatus_templates/Templaton/wp-content/plugins/realestate/registrationJson.php

Ajax代码:

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


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>

推荐答案

$_POST处理Content-type:application/x-www-form-urlencoded不是JSON来获取JSON使用

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

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

这篇关于无法在PHP中访问jquery Json POST参数,但使用Ajax和Normal post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆