[英]Why can't I get my JSON Data posted using AJAX in my PHP file?
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. 它运行正常,我可以看到Firebug控制台中发布的JSON数据。 The problem is with this PHP code:
问题出在这个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参数,并返回一个Undefined index: firstName in C:...
就像所有发布的那样参数。
I also tried using $data = $_POST['data']
and $_REQUEST['data']
to get all the JSON parameters and decode it using json_decode($data);
我还尝试使用
$data = $_POST['data']
和$_REQUEST['data']
来获取所有JSON参数并使用json_decode($data);
对其进行解码json_decode($data);
but didn't work. 但没有奏效。
I've also used the AJAX shortened code for post $.post('.././CheckPerson.php', {data: dataString}, function(res){ });
我还使用了AJAX缩短代码来发布
$.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. 我的PHP文件很好用,我的PHP文件现在可以读取
lastName
, firstName
和middleName
,但我认为它不是JSON数据而只是文本数据,因为firebug无法将其作为JSON数据读取。 Now,i'm confused how will my PHP file read the JSON data parameters.Do you guys have any suggestions about this? 现在,我很困惑我的PHP文件将如何读取JSON数据参数。你们有什么建议吗?
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. 问题是
dataType: "json"
并不意味着你发布了json,而是因为你的请求而希望从服务器接收json数据。 You could change your post data to: 您可以将帖子数据更改为:
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"];
One issue- you're using single quotes for your json. 一个问题 - 你正在为你的json使用单引号。 You should be using double quotes (according to spec).
你应该使用双引号(根据规范)。
{"lastName":"Smith", "firstName":"Joe"}
instead of
{'lastName':'Smith', 'firstName':'Joe'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.