te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Ajax return array shows [object Object],[object Object] in PHP - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ajax return array shows [object Object],[object Object] in PHP - Stack Overflow

programmeradmin2浏览0评论

Within Jquery I am creating two arrays, one embedded in the other, like so....

arrayOne = [{name:'a',value:1}, {name:'b',value:2}]
var arrayTwo = [{name:'foo',value:'blah'},{name:'arrayOne',value:arrayOne}];

I am then putting this though Ajax and extracting the variable via PHP on the other side. The results of a print_r($arrayTwo) are as follows...

Array([foo] => blah [arrayOne] => [object Object],[object Object])

I can see no way of extracting the contents of arrayOne, which is a pity because I really need them! Can anyone tell me what I am doing wrong in Jquery or what I need to do in PHP to make the embedded array accessible.

Many thanks as always


Edit to add my Ajax code....

$.ajax({
  type: "POST",
  url:'actions.php',
  data:arrayTwo,
  datatype:'json',
  cache: false,
  success: function(data){

}

})

Within Jquery I am creating two arrays, one embedded in the other, like so....

arrayOne = [{name:'a',value:1}, {name:'b',value:2}]
var arrayTwo = [{name:'foo',value:'blah'},{name:'arrayOne',value:arrayOne}];

I am then putting this though Ajax and extracting the variable via PHP on the other side. The results of a print_r($arrayTwo) are as follows...

Array([foo] => blah [arrayOne] => [object Object],[object Object])

I can see no way of extracting the contents of arrayOne, which is a pity because I really need them! Can anyone tell me what I am doing wrong in Jquery or what I need to do in PHP to make the embedded array accessible.

Many thanks as always


Edit to add my Ajax code....

$.ajax({
  type: "POST",
  url:'actions.php',
  data:arrayTwo,
  datatype:'json',
  cache: false,
  success: function(data){

}

})

Share Improve this question edited Nov 18, 2015 at 19:37 Jules asked Nov 18, 2015 at 19:25 JulesJules 2751 gold badge4 silver badges13 bronze badges 4
  • Seems like you are not serializing your data properly before you send it. [object Object] is the default string representation of a JavaScript object: console.log({}.toString()). Post the code you use to send the data. – Felix Kling Commented Nov 18, 2015 at 19:29
  • Which jQuery version are you using? – Felix Kling Commented Nov 18, 2015 at 19:38
  • print_r($arrayTwo['arrayOne']) just returns [object Object],[object Object] – Jules Commented Nov 18, 2015 at 19:38
  • JQuery Version 2.1.1 – Jules Commented Nov 18, 2015 at 19:40
Add a ment  | 

1 Answer 1

Reset to default 13

The issue is that jQuery's $.ajax (or rather $.param) method treats an array of objects in a special way. jQuery will use name as the parameter name and the string representation of value as value:

> $.param([{name: 'foo', value: 42}, {name: 'bar', value: 21}])
"foo=42&bar=21"

But the string representation of arrayOne is the useless stuff you are seeing on the server:

> [{name:'a',value:1}, {name:'b',value:2}].toString()
"[object Object],[object Object]"

The documentation actually points out the caveats when passing an array / object:

If the object passed is in an Array, it must be an array of objects in the format returned by .serializeArray()

[
  { name: "first", value: "Rick" },
  { name: "last", value: "Astley" },
  { name: "job", value: "Rock Star" }
]

Note: Because some frameworks have limited ability to parse serialized arrays, developers should exercise caution when passing an obj argument that contains objects or arrays nested within another array.

Note: Because there is no universally agreed-upon specification for param strings, it is not possible to encode plex data structures using this method in a manner that works ideally across all languages supporting such input. Use JSON format as an alternative for encoding plex data instead.


Since you have a plex data structure, you should probably use JSON to encode your data:

data: {data: JSON.stringify(arrayTwo)},

and on the server you simply decode it with

$data = json_decode($_POST['data'], true);

$data will have the exact same structure as arrayTwo.

But in case you want to actually have parameters with names foo and arrayOne, then you only need to serialize the the value of arrayOne:

data:  [
  {name:'foo',value:'blah'},
  {name:'arrayOne',value: JSON.stringify(arrayOne)}
],

and in PHP:

$arrayOne = json_decode($_POST['arrayOne'], true);
发布评论

评论列表(0)

  1. 暂无评论