下面是我的代码片段。首先,我循环遍历每个表行,获取第一、第二和第三个文本并将其推送到名为“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 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"]=> 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" } }
但似乎它不起作用,反而给我这个错误
Trying to get property of non-object
有什么帮助、线索、想法、建议、建议吗?
最佳答案
$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));
}
关于javascript - 使用 AJAX post 将对象数组发送到服务器端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37718157/