Home >Backend Development >PHP Tutorial > js里边的对象ajax post到php端直接变成数组了

js里边的对象ajax post到php端直接变成数组了

WBOY
WBOYOriginal
2016-06-13 12:38:04758browse

js里面的对象ajax post到php端直接变成数组了?

本帖最后由 zhoumengkang 于 2013-09-12 10:03:14 编辑 事先引入了jquery
var str ="{'a':'b','aa':'bb'}";<br />
var str2 = eval('('+str+')');<br />
var type = typeof(str2);<br />
console.log(str);<br />
console.log(type);//object<br />
console.log(str2);<br />
$.post('./bb.php',{'data':str2});

bb.php的代码
$data = $_POST['data'];<br />
var_dump($data);

控制台上看返回结果,直接输出为数组了
array(2) {<br />
  ["a"]=><br />
  string(1) "b"<br />
  ["aa"]=><br />
  string(2) "bb"<br />
}

我一般都是在php端需要json_decode()处理的,这样,不需要解析json格式,直接拿到的是数组了。

虽然一直知道js里对象就类似于php的关联数组,也不明白,这是什么原理呢(js里面的对象ajax post到php端直接变成数组了?)。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn