为什么我不能让我的PHP文件中使用AJAX我的JSON数据发布? [英] Why can't I get my JSON Data posted using AJAX in my PHP file?

查看:121
本文介绍了为什么我不能让我的PHP文件中使用AJAX我的JSON数据发布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

     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;
                    }

它工作正常,我可以看到JSON数据张贴在Firebug控制台。问题是这个PHP code:

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

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

以上PHP code似乎无法识别的firstName 的lastNamemiddleName作为贴JSON参数,并返回一个未定义指数:名字在C:... 类似的东西,所有发布的参数。

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.

我也尝试过使用 $数据= $ _ POST ['数据'] $ _ REQUEST ['数据'] 使用让所有的JSON参数和德code它 json_de code($的数据); ,但没有奏效。

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.

我还使用了AJAX缩短$ C $下交 $后期('.././ CheckPerson.php',{数据:dataString},功能(RES){} ); ,它的伟大工程与我的PHP文件和我的PHP文件现在可以读取的lastName 的firstName middleName ,但我认为它不是一个JSON数据,但只有一个文本数据,因为萤火虫无法读取它作为JSON数据。现在,我很困惑如何将我的PHP文件中读取JSON数据parameters.Do你们对此有什么建议?

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?

推荐答案

问题是,数据类型:JSON并不意味着你发布的JSON,但是,你期望从服务器接收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. 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"];

这篇关于为什么我不能让我的PHP文件中使用AJAX我的JSON数据发布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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