If I have, in javascript, something like:
如果我有,在javascript,类似:
entriesObj1 = new Object();
entriesObj1.entryId = "abc";
entriesObj1.mediaType = 2;
entriesObj2 = new Object();
entriesObj2.entryId = "def";
entriesObj2.mediaType = 1;
var entries = new Array();
entries[0] = entriesObj1;
entries[1] = entriesObj2;
What is the best method to pass it to php through an HTTP POST?
通过HTTP POST将其传递给php的最佳方法是什么?
I've tried a jQuery plugin to convert the array to JSON. I've tried to create multiple hidden fields named "entries[]", each one with the JSON string. Somehow, I can't seem to decode my data with PHP's json_decode
.
我尝试了一个jQuery插件将数组转换为JSON。我尝试创建多个名为“entries[]”的隐藏字段,每个字段都带有JSON字符串。不知何故,我似乎无法用PHP的json_decode解码我的数据。
EDIT: I tried changing the JSON plugin used to the one @Michal indicated and the results I get are the same:
编辑:我尝试将JSON插件改为@Michal所示,结果是一样的:
Javascript
Javascript
[
{"disciplina":"sdfsdfsdfsd","titulo":"sdfsdfsdf","componentes":"Bloco Completo"},
{"disciplina":"sdfsdfsdfsd","titulo":"sdfsdfsdf","componentes":"Bloco Completo"}
]
PHP Vardump:
PHP Vardump:
string(756) " [ {\"disciplina\":\"sdfsdfsdfsd\",\"titulo\":\"sdfsdfsdf\",\"componentes\":\"Bloco Completo\"}, {\"disciplina\":\"sdfsdfsdfsd\",\"titulo\":\"sdfsdfsdf\",\"componentes\":\"Bloco Completo\"} ] "
When I use PHP's json_decode
, I get NULL.
当我使用PHP的json_decode时,我得到NULL。
var_dump(json_decode($_REQUEST['entries']));
Output:
输出:
NULL
3 个解决方案
#1
1
You need to convert JSON to a string (use JSON stringifier (https://github.com/douglascrockford/JSON-js) and POST the string (as a field value) to the PHP script which does json_decode()
您需要将JSON转换为字符串(使用JSON stringifier (https://github.com/douglas douglas crockford/json -js)并将字符串(作为字段值)POST到PHP脚本,该脚本执行json_decode()
#2
1
Concat it using some characters like _
or %%
then pass it to PHP.
使用_或%%等字符进行Concat,然后将其传递给PHP。
In PHP file:
在PHP文件:
$ar = array();
$ar = explode('special_char',string pass from js);
echo "pre";print_r($ar);
echo "/pre";
#3
0
well, the trouble seemed to be with the quotes that were being passed in the post, so i just replaced the quotes with open strings to my $_REQUEST.
问题似乎在于post中传递的引号,所以我只是将引号替换为open string以满足$_REQUEST。
#1
1
You need to convert JSON to a string (use JSON stringifier (https://github.com/douglascrockford/JSON-js) and POST the string (as a field value) to the PHP script which does json_decode()
您需要将JSON转换为字符串(使用JSON stringifier (https://github.com/douglas douglas crockford/json -js)并将字符串(作为字段值)POST到PHP脚本,该脚本执行json_decode()
#2
1
Concat it using some characters like _
or %%
then pass it to PHP.
使用_或%%等字符进行Concat,然后将其传递给PHP。
In PHP file:
在PHP文件:
$ar = array();
$ar = explode('special_char',string pass from js);
echo "pre";print_r($ar);
echo "/pre";
#3
0
well, the trouble seemed to be with the quotes that were being passed in the post, so i just replaced the quotes with open strings to my $_REQUEST.
问题似乎在于post中传递的引号,所以我只是将引号替换为open string以满足$_REQUEST。