Laravel ajax 404 未找到错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38476348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel ajax 404 not found error
提问by user3714932
am getting an ajax 404 error while using laravel 5.0 and ajax and I cannot figure out why.
使用 laravel 5.0 和 ajax 时出现 ajax 404 错误,我不知道为什么。
My routes
我的路线
Route::post('user/saved/ [
'as' => 'delete-topics',
'uses' => 'TopicController@deleteTopic',
]);
My form
我的表格
<form action="{{action('TopicController@deleteTopic')}}" method="post" class="deleteform">
<input type="hidden" name="topic_id" value="{{$topic->topic_id}}">
<input type="submit" value="delete">
</form>
My ajax call
我的ajax调用
// process the form
$('.deleteform').submit(function(event) {
event.preventDefault();
var $form = $(this);
var formData = {
topic_id : $form.find('input[name=topic_id]').val()
};
// process the form
$.ajax({
type : 'POST',
url : 'saved',
data : formData,
dataType : 'json',
encode : true
})
My Controller
我的控制器
public function deleteTopic()
{
$topic_id = Request::get('topic_id');
if(Auth::check())
{
$user_id = Auth::user()->id;
$delete_topic = DB::table('topic_save')->where('user_id', $user_id)->where('topic_id', $topic_id)->delete();
}
$data['success'] = true;
$data['message'] = 'success';
echo json_encode($data);
}
I have tried changing the url in the ajax call to 'user/saved' to match the routes but am still getting the same 404 error
我尝试将 ajax 调用中的 url 更改为“用户/保存”以匹配路由,但仍然遇到相同的 404 错误
Thanks guys.
谢谢你们。
回答by trajchevska
Change the url to the action you have set for the form. That would be:
将 url 更改为您为表单设置的操作。那将是:
url = $form.attr('action');
$.ajax({
type : 'POST',
url : url,
data : formData,
dataType: 'json',
encode : true
})
回答by Raymond Cheng
you can just remove action property from your Form, because you don't use form submit, then change ajax like this:
您可以从表单中删除 action 属性,因为您不使用表单提交,然后像这样更改 ajax:
$.ajax({
type : 'POST',
url : "{{ route('delete-topics') }}",
data : formData,
dataType: 'json',
encode : true
})
回答by Mayank Pandeyz
Your form action itself is incorrect:
您的表单操作本身不正确:
<form action="{{action('TopicController@deleteTopic')}}" method="post" class="deleteform">
instead of this use:
而不是这种使用:
action="route_path"