将JSON(jQuery)发送到PHP并对其进行解码

时间:2021-07-04 07:25:44

I can't for the life of me figure out what I'm doing wrong. It seems like it should be simple because I can't find anyone else with this issue but I can't figure out to send basic data via javascript(jQuery) to PHP and decode it. For the sake of simplicity, this is what I have:

我不能为我的生活弄清楚我做错了什么。它似乎应该很简单,因为我找不到其他任何人有这个问题,但我不知道通过javascript(jQuery)发送基本数据到PHP并解码它。为了简单起见,这就是我所拥有的:

JAVASCRIPT

var json_data = { "name" : "john doe" };

$.ajax({
    type: "POST",
    url: "../bin/process.php",
    dataType: "json",
    data: json_data
    });

and my PHP FILE

和我的PHP文件

$arr = json_decode("json_data", true);

$fp = fopen('data.txt', "w");
fwrite($fp, $arr['name']);
fclose($fp);

The file I'm writing ends up with nothing in it. If I do an:

我正在写的文件最终没有任何内容。如果我这样做:

fwrite($fp, 'test');

I get a file with the word test in it but no matter what I do I don't get the json data I sent.

我得到一个带有单词test的文件,但无论我做什么,我都没有得到我发送的json数据。

Can someone please share a thorough example of A to Z. Thanks for any help.

有人可以分享A到Z的完整示例。感谢您的帮助。

2 个解决方案

#1


5  

The ajax request that you make with jQuery will send the parameter 'name', with the value 'john doe', and not the whole object. If you want to send the whole object, you have to pass it like this:

使用jQuery创建的ajax请求将发送参数'name',其值为'john doe',而不是整个对象。如果要发送整个对象,则必须像这样传递:

data: { parameters: json_data }

On the PHP side, you can get the variables from the $_POST superglobal. Using your example, you would use:

在PHP方面,您可以从$ _POST超全局获取变量。使用您的示例,您将使用:

$name = $_POST['name'];

Or, if you send the whole object, using my example:

或者,如果您使用我的示例发送整个对象:

$params = $_POST['parameters'];

There is no need to use json_decode(), since the parameters you pull out from the $_POST array will be native PHP variables already.

不需要使用json_decode(),因为从$ _POST数组中提取的参数已经是本机PHP变量。

You only need to use it, if you have a json string, which you want to turn into a PHP variable, which is not the case here, since jQuery will "transform" the javascript object, to a query string in the background.

你只需要使用它,如果你有一个json字符串,你想把它变成一个PHP变量,这不是这里的情况,因为jQuery会将javascript对象“转换”为后台的查询字符串。

It is a rare case that you need to send data from javascript in a JSON form, but if you want to do that you need something like:

您需要以JSON格式从javascript发送数据是一种罕见的情况,但如果您想这样做,则需要以下内容:

// JS

var person = "{ name: 'John Doe' }"; // notice the double quotes here, this is a string, and not an object
$.ajax({
    type: "POST",
    url: "../bin/process.php",
    dataType: "json",
    data: { json: person }
    });

// PHP

$json = $_POST['json']; // $json is a string
$person = json_decode($json); // $person is an array with a key 'name'

#2


1  

jQuery cannot encode data to JSON, only decode it (the poorly named dataType parameter actually refers to the type of the reponse). Use json2.js for encoding.

jQuery无法将数据编码为JSON,只能对其进行解码(命名不佳的dataType参数实际上是指响应的类型)。使用json2.js进行编码。

#1


5  

The ajax request that you make with jQuery will send the parameter 'name', with the value 'john doe', and not the whole object. If you want to send the whole object, you have to pass it like this:

使用jQuery创建的ajax请求将发送参数'name',其值为'john doe',而不是整个对象。如果要发送整个对象,则必须像这样传递:

data: { parameters: json_data }

On the PHP side, you can get the variables from the $_POST superglobal. Using your example, you would use:

在PHP方面,您可以从$ _POST超全局获取变量。使用您的示例,您将使用:

$name = $_POST['name'];

Or, if you send the whole object, using my example:

或者,如果您使用我的示例发送整个对象:

$params = $_POST['parameters'];

There is no need to use json_decode(), since the parameters you pull out from the $_POST array will be native PHP variables already.

不需要使用json_decode(),因为从$ _POST数组中提取的参数已经是本机PHP变量。

You only need to use it, if you have a json string, which you want to turn into a PHP variable, which is not the case here, since jQuery will "transform" the javascript object, to a query string in the background.

你只需要使用它,如果你有一个json字符串,你想把它变成一个PHP变量,这不是这里的情况,因为jQuery会将javascript对象“转换”为后台的查询字符串。

It is a rare case that you need to send data from javascript in a JSON form, but if you want to do that you need something like:

您需要以JSON格式从javascript发送数据是一种罕见的情况,但如果您想这样做,则需要以下内容:

// JS

var person = "{ name: 'John Doe' }"; // notice the double quotes here, this is a string, and not an object
$.ajax({
    type: "POST",
    url: "../bin/process.php",
    dataType: "json",
    data: { json: person }
    });

// PHP

$json = $_POST['json']; // $json is a string
$person = json_decode($json); // $person is an array with a key 'name'

#2


1  

jQuery cannot encode data to JSON, only decode it (the poorly named dataType parameter actually refers to the type of the reponse). Use json2.js for encoding.

jQuery无法将数据编码为JSON,只能对其进行解码(命名不佳的dataType参数实际上是指响应的类型)。使用json2.js进行编码。