2016-06-09 69 views
1

下面是我的代码片段。首先,我通过每个表行环,获得第一,第二和第三文本,并将其推到阵列名为“文件”(如多维数组,你可以看到它的控制台日志)使用AJAX发送一组对象到服务器端

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)); 
} 

如果我倾倒名为 '身份证' 的要求这里是我得到

阵列(3){[0] =>阵列(4){[ “名称”] =>字符串(18)“名称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 “] =>串(7) ”名称3“ [” 年龄 “] =>串(3) ”40“[” 同一性“] =>串(7) ”异形“}}

但似乎它不工作,而是它给了我■错误

试图让非对象的属性

任何帮助,线索,意见,建议,建议好吗? 。

+0

倾倒在第一的请求...... – vitr

+0

@vitr:请参阅我的更新后。 –

+1

你试过这个$ count。= $ d [“identity”]; –

回答

3

$计数= $ d =>身份$计数= $ d [ “身份证”]

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