我想从表中删除一行使用jquery ajax。经过几次尝试,我不知道为什么我的代码(删除部分)不工作,因为我是新的Ajax和JavaScript。从服务器加载ajax的数据工作正常,脚本没有控制台错误。当我按删除时,我在网络标签上看不到任何东西。这里是我的代码:Laravel简单的毛病用jQuery的ajax
routes.php文件
Route::delete('users/{id}','[email protected]');
AjaxController.php
public function destroy($id)
{
$user = User::findOrFail($id);
$user->delete();
}
观点:
<table id="users" class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Action</th>
</tr>
</thead>
<tbody id="abc">
</tbody>
</table>
脚本:
$(document).ready(function(){
var $tbody = $('#abc');
// getting data from server
$.ajax({
type : 'GET',
url : 'api/users',
success: function(users) {
$.each(users, function(i, user){
$tbody.append('<tr id="user_' + user.id + '"><td>'+user.name+'</td><td>'+user.phone+'</td><td><button type="button" class="btn btn-xs btn-danger" id="delete" value="'+user.id+'" name="button">Delete</button></td></tr>');
});
},
error: function(){
alert('error loading data');
}
});
// deleting data
$('#delete').on('click', function(e){
var user_id = $(this).val();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
})
e.preventDefault();
var user = {
id : user_id
};
$.ajax({
type: 'DELETE',
url : '/user/'+user.id,
success : function(deleteUser)
{
$("#user_" + user_id).remove();
},
error : function()
{
alert('error deleting data');
}
});
}); // deleting block ends
});
});
http://itsolutionstuff.com/post/laravel-5-ajax-crud-with-pagination-example-and-demo-from-scratchexample.html – 2016-08-12 04:55:32
你确定,你正在得到正确的'$ id'在你的'destroy'函数中。尝试打印它。 –
当我按删除,我看不到任何网络选项卡(控制台)。 @ravishankar –