JavaScript对象使用jQuery AJAX到PHP [英] javascript object to php using jquery ajax
问题描述
我有我创建这样一个数组或一个JavaScript对象:
改编[arr.length] = OBJ
其中obj是一个典型的JSON字符串如 {ID:1}
所以改编
似乎是JavaScript的对象的数组。
我可以访问它像这样:
改编[1]
,改编[2]
。
它可能会更喜欢警报(ARR [1] .ID);
如果我这样做: 警报(JSON.stringify(ARR)); 我得到了以下内容:
<$p$p><$c$c>[{"id":"2305","col":"1"},{"id":"2308","col":"1"},{"id":"2307","col":"1"},{"id":"2306","col":"1"}]
而 警报(ARR); 给我的是这样的:
[对象对象],[对象的对象],[对象的对象],[对象的对象],[对象的对象]
现在我需要它传递给使用jQuery的AJAX方法,一个PHP脚本。但它似乎只能得到合并,例如字符串:
{ID:2305,山口:1}
或 {ID:2305, COL:1,ID:2305,山口:1}
但JSON.stringify解析改编对象成功和我的$ P $字符串我有pvious例子似乎是一个有效的JSON字符串。如何传递到PHP,我应该真正改变结构的全格式要像过去的样品?
UPD: 我忘了提,PHP的POST数组为null,如果发送{},{},{}'字符串给它而不是{}字符串。
UPD: 我重写时生成的字符串code。现在我有一个像这样的字符串:
{2305:1,2306:1}
它的工作原理,如果我把它传递给PHP直接,像这样的:
$。员额(网址:{url:../getItems2Cart.php,数据:{2305:1,2306:1},
成功:函数(响应){警报(响应);}
});
如果我把它这样,PHP返回空POST数组:
$交(网址:{url:../getItems2Cart.php,数据:JSON.stringify(STR),.
成功:函数(响应){警报(响应);}
});
要获得清晰,警觉现在返回正确的JSON强:
警报(JSON海峡到PHP+ JSON.stringify(STR));
// JSON海峡到PHP {2305:1,2306:1}
啊...是的,和str是一个JavaScript对象,而不是字符串。
可以发送JSON,并使用 json_de code()
来把它变成一个PHP数组
$后期('服务器/路径,{jsonData:JSON.stringify(ARR)},功能(响应){
/ *如果需要做一些与响应* /
});
在PHP中:
$ ARR = json_de code($ _ POST ['jsonData']);
/ *返回第一个ID为测试* /
回声$ ARR [0] ['身份证'];
/ *或倾倒整个数组作为回应AJAX:* /
的print_r($ ARR);
I have an array or a javascript object that I create like this:
arr[arr.length]=obj
where the obj is a classic JSON string like {"id":1}
.
So arr
seems to be an array of JavaScript Objects.
I can access to it like this:
arr[1]
, arr[2]
.
It could be even like alert(arr[1].id);
If i do: alert(JSON.stringify(arr)); I get the following:
[{"id":"2305","col":"1"},{"id":"2308","col":"1"},{"id":"2307","col":"1"},{"id":"2306","col":"1"}]
Whereas alert(arr); Gives me something like:
[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:
{"id":"2305","col":"1"}
or {"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?
UPD: i forgot to mention that PHP's POST array is null if send '{},{},{}' string to it rather then '{}' string.
UPD: I rewrote the code that was generated the string. Now i have a string like this:
{"2305":"1","2306":"1"}
It works if i pass it to PHP directly, like this:
$.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:
$.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 str to php '+JSON.stringify(str));
//json str to php {"2305":"1","2306":"1"}
Ahh.. yes, and str is an javascript object, not string.
Can send the JSON and use json_decode()
to turn it into a php array.
$.post('server/path', { jsonData: JSON.stringify(arr)}, function(response){
/* do something with response if needed*/
});
in 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);
这篇关于JavaScript对象使用jQuery AJAX到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!