【发布时间】:2020-02-09 00:13:32
【问题描述】:
这是我的代码。
$('.btn-danger').click(function () {
id=$(this).attr('id');
alertify.confirm("Want to delete?", "It's not possible to turn back!",
function () {
$.ajax({
type: 'DELETE',
url: '/blog/'+ id,
success: function (msg) {
alert(msg)
if (msg){
$("item-"+id).remove()
alertify.success('Done!')
}
else{
alertify.error('There's an error!')
}
},
error: function (jqXHR, textStatus, errorThrown) {
alertify.error('error var' + 'blog/'+id)
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
alert(msg)
}
})
},
function () {
alertify.error('Deleting cancelled.')
}
)
})
这是我的路线。
Route::namespace('AdminPanel')->group(function (){
Route::prefix('admin')->group(function (){
Route::resource('blog','BlogController');
});
});
这是我的销毁方法。
public function destroy($id)
{
$blog = Blogs::find(intval($id));
if ($blog->delete()){
echo 1;
}
echo 0;
}
这是我的路线列表(用于回答这个问题):
但每次我尝试单击删除并说好的,ajax 无法使用成功功能。出现错误:“未找到请求的页面。[404]”。
我该如何解决这个问题?
【问题讨论】:
-
你的路线在哪里,在 web.php 中?如果是这样,您需要添加 _token 字段,以及 X-CSRF-TOKEN 和标头。请运行
php artisan route:list并将结果添加到问题中。 -
@porloscerros 我用另一个答案解决了它。我还添加了路线:列出问题。如果你愿意,你可以看看。谢谢兄弟,祝你有个愉快的一天
标签: ajax laravel controller destroy