使用ajax从路由中调用删除url?
如何使用ajax从路由调用url 路线:使用ajax从路由中调用删除url?,ajax,laravel,laravel-5,Ajax,Laravel,Laravel 5,如何使用ajax从路由调用url 路线: Route::delete('dashboard/tags/destroy/{tag}', 'TagCon@destroy')->name('tagdestroy'); 控制器: public function destroy($id) { $tag = Tag::findOrFail($id); $tag->delete(); return response()->json($tag
Route::delete('dashboard/tags/destroy/{tag}', 'TagCon@destroy')->name('tagdestroy');
控制器:
public function destroy($id) {
$tag = Tag::findOrFail($id);
$tag->delete();
return response()->json($tag);
}
Ajax代码:
$(document).on('click', '.delete-modal', function () {
$('.modal-title').text('Delete');
$('#id_delete').val($(this).data('id'));
$('#title_delete').val($(this).data('title'));
$('#deleteModal').modal('show');
id = $('#id_delete').val();
});
$('.modal-footer').on('click', '.delete', function () {
$.ajax({
type: 'DELETE',
url: 'dashboard/tags/destroy/' + id,
data: {
'_token': $('input[name=_token]').val(),
},
success: function (data) {
toastr.success('Successfully deleted Post!', 'Success Alert', {timeOut: 5000});
$('.item' + data['id']).remove();
$('.col1').each(function (index) {
$(this).html(index + 1);
});
}
});
});
它什么也不做也许是因为它没有呼叫正确的路线?但是,如果我将此url:'dashboard/tags/destroy/'+id,
更改为此url:{{url::route('tagdestroy')}}+id,
将返回一个错误缺少[route:tagdestroy][URI URI dashboard/tags/destroy/{tag}]所需的参数。
代码的错误部分在哪里?谢谢
已解决
现在可以工作了,将ajax函数中的url调用更改为
url:'tags/destroy/'+id,
,而不是url:'dashboard/tags/destroy/'+id,
。我不知道它为什么工作,删除方法url仍然定义为Route::delete('dashboard/tags/destroy/{tag}','TagCon@destroy')->name('tagdestroy')代码>,而不是Route::delete('tags/destroy/{tag}','TagCon@destroy')->name('tagdestroy')代码>
您的命名路由中缺少参数。我认为第一个url运行良好。
您是否仔细检查id
参数?
我看到在ajax函数中,您没有声明它。
您在不同的函数中调用id
?uri中的标记是否必要?在我的ajax调用中,我使用它作为POST参数来省略它。
路线:
Ajax调用:
$(document).on('click', '.delete-modal', function () {
$('.modal-title').text('Delete');
$('#id_delete').val($(this).data('id'));
$('#title_delete').val($(this).data('title'));
$('#deleteModal').modal('show');
id = $('#id_delete').val();
});
$('.modal-footer').on('click', '.delete', function () {
$.ajax({
type: 'DELETE',
url: '{{route('tagdestroy')}}',
data: {
'_token': $('input[name=_token]').val(), 'id': id
},
success: function (data) {
toastr.success('Successfully deleted Post!', 'Success Alert', {timeOut: 5000});
$('.item' + data['id']).remove();
$('.col1').each(function (index) {
$(this).html(index + 1);
});
}
});
});
控制器:
public function destroy() {
$id = e(Input::get('id'));
$tag = Tag::findOrFail($id);
$tag->delete();
return response()->json($tag);
}
我确实包含了参数url:{{url::route('tagdestroy')}}+id,
。id是参数。也许我用错误的方式包含了参数?您可以将其设置为可选的,然后您可以像前面提到的那样传递它,但是您需要更新您的路由仪表板/tags/destroy/{tag?}
,我确实按照您所说的那样更改了路由仪表板/tags/destroy/{tag?}
,但它仍然没有做任何事情。我被卡住了:(我确实声明了它id=$(“#id_delete”).val()
@SandraVioletta:你能试试Route::post()
吗?在ajax中键入:“post”`试过了,但还是什么都没做,没有错误只是没有删除data@SandraVioletta在标记网络中打开chrome的web developer?,单击“删除”。您看到了什么?(请参阅参数仪表板/标签/destroy/
)嗯,我得到了这个http://mk1.dev/dashboard/dashboard/tags/destroy/18
出现错误404状态代码。是的,需要通过将id作为参数来确定删除了哪些数据。现在,它开始工作,将ajax函数中的url调用更改为url:'tags/destroy/'+id,
而不是url:'dashboard/tags/destroy/'+id,
。我不知道它为什么会起作用,但在路由中,url仍然被定义为“url:”dashboard/tags/destroy/{tag}。无论如何,非常感谢!
$(document).on('click', '.delete-modal', function () {
$('.modal-title').text('Delete');
$('#id_delete').val($(this).data('id'));
$('#title_delete').val($(this).data('title'));
$('#deleteModal').modal('show');
id = $('#id_delete').val();
});
$('.modal-footer').on('click', '.delete', function () {
$.ajax({
type: 'DELETE',
url: '{{route('tagdestroy')}}',
data: {
'_token': $('input[name=_token]').val(), 'id': id
},
success: function (data) {
toastr.success('Successfully deleted Post!', 'Success Alert', {timeOut: 5000});
$('.item' + data['id']).remove();
$('.col1').each(function (index) {
$(this).html(index + 1);
});
}
});
});
public function destroy() {
$id = e(Input::get('id'));
$tag = Tag::findOrFail($id);
$tag->delete();
return response()->json($tag);
}