简体   繁体   English

Laravel Ajax 404未找到错误

[英]Laravel ajax 404 not found error

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更改为“ user / saved”以匹配路由,但仍然遇到相同的404错误

Thanks guys. 多谢你们。

Change the url to the action you have set for the form. 将url更改为您为表单设置的操作。 That would be: 那将是:

url = $form.attr('action');
$.ajax({
  type    : 'POST', 
  url     : url,
  data    : formData, 
  dataType: 'json', 
  encode  : true
})

you can just remove action property from your Form, because you don't use form submit, then change ajax like this: 您可以只从表单中删除操作属性,因为您不使用表单提交,然后像这样更改ajax:

$.ajax({
  type    : 'POST', 
  url     : "{{ route('delete-topics') }}",
  data    : formData, 
  dataType: 'json', 
  encode  : true
})

Your form action itself is incorrect: 您的表单操作本身是错误的:

<form action="{{action('TopicController@deleteTopic')}}" method="post" class="deleteform">

instead of this use: 代替这种用法:

action="route_path"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM