【问题标题】:Send an array of objects to the server side using AJAX post使用 AJAX post 将对象数组发送到服务器端
【发布时间】:2016-10-09 15:30:09
【问题描述】:

下面是我的sn-p。首先,我循环遍历每个表格行,获取第一、第二和第三个文本并将其推送到名为“文件”的数组中(如多维数组,您可以查看它的控制台日志)

var files = []
$(document).ready(function(){
  $('table tr').each(function(){
    files.push({ 'name' : $(this).find('td:first-child').text(), 'age' : $(this).find('td:nth-child(2)').text(), 'identity' : $(this).find('td:nth-child(3)').text() });
  
  });
  console.log(files);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<table>
  <tr>
    <td>Name 1</td>
    <td>22</td>
    <td>Human</td>
  </tr>
    <tr>
    <td>Name 2</td>
    <td>18</td>
    <td>Human</td>
  </tr>
  <tr>
    <td>Name 3</td>
    <td>40</td>
    <td>Alien</td>
  </tr>
</table>

然后使用 Ajax post 发送它

$.ajax({
    url:'/page.php',
    type:'post',
    dataType:'json',
    data: { id : files },
    success:function(e){}
});

然后在后端侧

public function rr(Request $request){
    $count = '';
    //loop
    foreach($request->id as $d){
       $count.=$d->identity;
    }
    dd(var_dump($count));
}

如果我转储名为“id”的请求,这就是我得到的

array(3) { [0]=> array(4) { ["name"]=> string(18) "Name 1" ["age"]=> 字符串(3)“22”[“身份”]=>字符串(18)“人类”}[1]=>数组(4){ ["name"]=> string(14) "姓名 2" ["age"]=> string(3) "18 ["identity"]=> 字符串(14)“人类”} [2]=> 数组(4){ [“名称”]=> 字符串(7)“名称 3” ["age"]=> string(3) "40" ["identity"]=> string(7) "外星人" } }

但似乎它不起作用,而是给了我这个错误

试图获取非对象的属性

有什么帮助、线索、想法、建议、建议吗?

【问题讨论】:

  • 首先转储请求...
  • @vitr:请看我更新的帖子。
  • 你试过这个 $count.=$d["identity"];
  • @MuhammadUsman:哇!谢谢,它有效。成功了!

标签: javascript jquery ajax laravel laravel-5


【解决方案1】:

$count.=$d=>identity$count.=$d["identity"]

  public function rr(Request $request){
    $count = '';
   //loop
    foreach($request->id as $d){
      $count.=$d["identity"];
   }
    dd(var_dump($count));
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    相关资源
    最近更新 更多