簡體   English   中英

使用AJAX post將一組對象發送到服務器端

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

以下是我的片段。 首先,我循環遍歷每個表行,獲取第一個,第二個和第三個文本並將其推送到名為'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> 

然后使用Ajax發送它

$.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”] => 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“}}

但似乎它不起作用,相反它給了我這個錯誤

試圖獲得非對象的屬性

有任何幫助,線索,想法,建議和建議嗎?

$ 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