[英]javascript objects to json string to php array -> POST
Hey guys i really need help with this. 嘿伙计我真的需要帮助。 i pass this json object to php..
我把这个json对象传递给php ..
var x = {};
x.xt = {};
x.xt.id = id;
x.xt.to = foo;
somearray.push(x);
convert object to json: 将对象转换为json:
$.toJSON(x);
json string: json字符串:
[{"x":{"xt":"9","to":"2"}}]
them i post this: 他们发布这个:
$.post(
"temp/sop.php",
{ xa: somearray},
function(data){
console.log("response - "+ data);
});
server side: 服务器端:
$xtj = $_POST["xa"];
$encodedArray = array_map(utf8_encode, $xtj);
$asnk = json_decode($encodedArray);
This returns: 返回:
string(4) "null"
and this: 和这个:
$asnk = json_encode($xtj);
returns: 收益:
null
the data base it is set to: 它设置的数据库:
UTF8
UTF8
also when i test if it is an array, comes back true.. 当我测试它是否是一个数组时,回来是真的..
any idea how to solve this? 任何想法如何解决这个问题? thanks
谢谢
also server side: 服务器端:
$xtj = $_POST["xa"];
$asnk = json_decode($xtj);
this returns: 这会返回:
NULL
$.toJSON(x)
does not do the conversion in-place; $.toJSON(x)
不会就地进行转换; it returns the JSON, and you're just discarding it. 它返回 JSON,你只是丢弃它。 You need this instead:
你需要这个:
$.post(
"temp/sop.php",
{ xa: $.toJSON(somearray) },
// ...
});
Then, on the PHP side, you won't want array_map
as it's not going to be an array until you decode the JSON: 然后,在PHP方面,你不需要
array_map
因为在你解码JSON之前它不会是一个数组:
$xtj = $_POST["xa"];
$encodedArray = utf8_encode($xtj); // I'm not sure you need this, by the way.
$asnk = json_decode($encodedArray);
try using if(get_magic_quotes_gpc()) $xtj = stripslashes($xtj);
尝试使用
if(get_magic_quotes_gpc()) $xtj = stripslashes($xtj);
to lose the excessive escaping before trying to decode. 在尝试解码之前失去过多的逃逸。
What you are doing is you are converting to json string in JS ( $.toJSON()
). 你正在做的是你转换为JS中的json字符串(
$.toJSON()
)。 And then in PHP you are again trying to convert to json string ( json_encode()
). 然后在PHP中你再次尝试转换为json字符串(
json_encode()
)。 And you are using array_map()
on something that is not an array but a string. 并且你在不是数组而是字符串的东西上使用
array_map()
。 (Try echo $_POST["xa"];
to see the contents of it.) (尝试
echo $_POST["xa"];
查看它的内容。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.