简体   繁体   English

使用AJAX post将一组对象发送到服务器端

[英]Send an array of objects to the server side using AJAX post

Below is my snippet. 以下是我的片段。 First, I loop through each table row, get the first, second and third text and push it to the array named 'files' (like multi dimensional array, you can see its console log) 首先,我循环遍历每个表行,获取第一个,第二个和第三个文本并将其推送到名为'files'的数组(如多维数组,您可以看到它的控制台日志)

 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> 

and then send it using Ajax post 然后使用Ajax发送它

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

and then in the back end side 然后在后端

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

If I dump the request named 'id' here's what I get 如果我转储名为'id'的请求,这就是我得到的

array(3) { [0]=> array(4) { ["name"]=> string(18) "Name 1" ["age"]=> string(3) "22" ["identity"]=> string(18) "Human" } [1]=> array(4) { ["name"]=> string(14) "Name 2" ["age"]=> string(3) "18 ["identity"]=> string(14) "Human" } [2]=> array(4) { ["name"]=> string(7) "Name 3" ["age"]=> string(3) "40" ["identity"]=> string(7) "Alien" } } array(3){[0] => array(4){[“name”] => string(18)“Name 1”[“age”] => string(3)“22”[“identity”] = > string(18)“Human”} [1] => array(4){[“name”] => string(14)“Name 2”[“age”] => string(3)“18 [”identity “] => string(14)”Human“} [2] => array(4){[”name“] => string(7)”名称3“[”年龄“] =>字符串(3)”40 “[”identity“] => string(7)”Alien“}}

but seems like its not working, instead it gives me this error 但似乎它不起作用,相反它给了我这个错误

Trying to get property of non-object 试图获得非对象的属性

Any help, clues, ideas, suggestions, recommendations please? 有任何帮助,线索,想法,建议和建议吗?

$count.=$d=>identity to $count.=$d["identity"] $ count。= $ d =>身份$ count。= $ d [“身份”]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM