使用 jquery ajax 将 javascript 对象转换为 php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14183837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:56:51  来源:igfitidea点击:

javascript object to php using jquery ajax

phpjavascriptjqueryajaxjson

提问by user1953035

I have an array or a javascript object that I create like this: arr[arr.length]=objwhere the obj is a classic JSON string like {"id":1}.

我有一个数组或一个 javascript 对象,我像这样创建: arr[arr.length]=obj其中 obj 是一个经典的 JSON 字符串,如{"id":1}.

So arrseems to be an array of JavaScript Objects.

所以arr似乎是一个 JavaScript 对象数组。

I can access to it like this: arr[1], arr[2]. It could be even like alert(arr[1].id);

我可以这样访问它: arr[1], arr[2]。它甚至可能像alert(arr[1].id);

If i do: alert(JSON.stringify(arr)); I get the following:

如果我这样做: alert(JSON.stringify(arr)); 我得到以下信息:

[{"id":"2305","col":"1"},{"id":"2308","col":"1"},{"id":"2307","col":"1"},{"id":"2306","col":"1"}]

Whereas alert(arr); Gives me something like:

而 alert(arr); 给了我类似的东西:

[object Object],[object Object],[object Object],[object Object],[object Object]

Now I need to pass it to a PHP script using jQuery's AJAX method. But it seems that it can get only combined strings like:

现在我需要使用 jQuery 的 AJAX 方法将它传递给 PHP 脚本。但它似乎只能得到组合字符串,如:

{"id":"2305","col":"1"}or {"id":"2305","col":"1","id":"2305","col":"1"}

{"id":"2305","col":"1"}或者 {"id":"2305","col":"1","id":"2305","col":"1"}

But JSON.stringify parses the arr object successfully and my previous example of string I have seems to be a valid JSON string. How can I pass to PHP, should I really change the whole format of structure to be like the last sample?

但是 JSON.stringify 成功解析了 arr 对象,我之前的字符串示例似乎是一个有效的 JSON 字符串。我如何传递给 PHP,我真的应该将整个结构格式更改为最后一个示例吗?

UPD: i forgot to mention that PHP's POST array is null if send '{},{},{}' string to it rather then '{}' string.

UPD:我忘了提到如果向 PHP 发送 '{},{},{}' 字符串而不是 '{}' 字符串,则 PHP 的 POST 数组为空。

UPD: I rewrote the code that was generated the string. Now i have a string like this:

UPD:我重写了生成字符串的代码。现在我有一个这样的字符串:

{"2305":"1","2306":"1"}

It works if i pass it to PHP directly, like this:

如果我直接将它传递给 PHP,它会起作用,如下所示:

   $.post({url: '../getItems2Cart.php', data:{"2305":"1","2306":"1"} ,
       success: function(response){alert(response);}
   });

If i send it like this, php return empty POST array:

如果我像这样发送它,php 返回空的 POST 数组:

$.post({url: '../getItems2Cart.php', data: JSON.stringify(str),.
       success: function(response){alert(response);}
});

To get clear, alert returns a proper JSON strong now:

为了清楚起见,alert 现在返回一个正确的 JSON 强:

alert('json str to php '+JSON.stringify(str));
//json str to php {"2305":"1","2306":"1"}

Ahh.. yes, and str is an javascript object, not string.

啊.. 是的,str 是一个 javascript 对象,而不是字符串。

回答by charlietfl

Can send the JSON and use json_decode()to turn it into a php array.

可以发送 JSON 并用于json_decode()将其转换为 php 数组。

$.post('server/path', { jsonData: JSON.stringify(arr)}, function(response){
   /* do something with response if needed*/
});

in php:

在 php 中:

$arr=json_decode( $_POST['jsonData'] );
/* return first ID as test*/
echo $arr[0]['id'];
/* or dump whole array as response to ajax:*/
print_r($arr);