为什么我不能在PHP文件中使用AJAX发布JSON数据呢?

时间:2022-10-08 10:05:10

I have an AJAX script that post data in one of my PHP file:

我有一个AJAX脚本,在我的PHP文件中发布数据:

     var _lname = $('#ptLastName').val();
    var _fname = $('#ptFirstName').val();
    var _mname = $('#ptMiddleName').val();
$.ajax({
                type: "POST",
                url: ".././CheckPerson.php",
                data: "{'lastName':'" + _lname + "','firstName':'" + _fname + "','middleName':'" + _mname + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var res = response.d;
                    if (res == true) {
                        jAlert('Person Name already exists!', 'Error');
                        return;
                    }

It works fine and I can see the JSON Data posted in the Firebug console. The problem is with this PHP code:

它工作得很好,我可以看到在Firebug控制台中张贴的JSON数据。这个PHP代码的问题是:

$firstname = json_decode($_POST['firstName']);
$lastname = json_decode($_POST['lastName']);
$middlename = json_decode($_POST['middleName']);
$response = array();

The above PHP code seems that it can't recognize the 'firstName','lastName', and 'middleName' as a posted JSON parameter, and return an Undefined index: firstName in C:... something like that for all the posted parameters.

上面的PHP代码似乎无法将“firstName”、“lastName”和“middleName”识别为已发布的JSON参数,并返回C:…对于所有发布的参数都是这样。

I also tried using $data = $_POST['data'] and $_REQUEST['data'] to get all the JSON parameters and decode it using json_decode($data); but didn't work.

我还尝试使用$data = $_POST['data']和$_REQUEST['data']获取所有JSON参数,并使用json_decode($data)对其进行解码;但没有工作。

I've also used the AJAX shortened code for post $.post('.././CheckPerson.php', {data: dataString}, function(res){ });, it works great with my PHP file and my PHP file can now read lastName, firstName, and middleName, but i think it is not a JSON data but only a text data because firebug can't read it as JSON data. Now,i'm confused how will my PHP file read the JSON data parameters.Do you guys have any suggestions about this?

我还使用了post $.post的AJAX缩短代码('.././CheckPerson)。php', {data: dataString}, function(res){});,它对我的php文件非常有用,我的php文件现在可以读取lastName、firstName和middleName,但我认为它不是JSON数据,而是文本数据,因为firebug不能读取JSON数据。现在,我很困惑,我的PHP文件将如何读取JSON数据参数。你们对此有什么建议吗?

2 个解决方案

#1


7  

The problem is that dataType: "json" doesn't mean that you're posting json, but that you're expecting to receive json data from the server as a result of your request. You could change your post data to:

问题是,dataType:“json”并不意味着您发布了json,而是您希望作为请求的结果从服务器接收json数据。你可以把你的文章数据改为:

data: {myPostData : "{'lastName':'" + _lname + "','firstName':'" + _fname + "','middleName':'" + _mname + "'}"}

and then parse it on your server like

然后像这样在服务器上解析它

$myPostData = json_decode($_POST['myPostData']);
$firstname = $myPostData["firstName"];
$lastname = $myPostData["lastName"];
$middlename = $myPostData["middleName"];

#2


2  

One issue- you're using single quotes for your json. You should be using double quotes (according to spec).

一个问题——json使用的是单引号。您应该使用双引号(根据规范)。

{"lastName":"Smith", "firstName":"Joe"}

instead of 

{'lastName':'Smith', 'firstName':'Joe'}

#1


7  

The problem is that dataType: "json" doesn't mean that you're posting json, but that you're expecting to receive json data from the server as a result of your request. You could change your post data to:

问题是,dataType:“json”并不意味着您发布了json,而是您希望作为请求的结果从服务器接收json数据。你可以把你的文章数据改为:

data: {myPostData : "{'lastName':'" + _lname + "','firstName':'" + _fname + "','middleName':'" + _mname + "'}"}

and then parse it on your server like

然后像这样在服务器上解析它

$myPostData = json_decode($_POST['myPostData']);
$firstname = $myPostData["firstName"];
$lastname = $myPostData["lastName"];
$middlename = $myPostData["middleName"];

#2


2  

One issue- you're using single quotes for your json. You should be using double quotes (according to spec).

一个问题——json使用的是单引号。您应该使用双引号(根据规范)。

{"lastName":"Smith", "firstName":"Joe"}

instead of 

{'lastName':'Smith', 'firstName':'Joe'}