2

am getting an ajax 404 error while using laravel 5.0 and ajax and I cannot figure out why.

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

  // 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

Thanks guys.

1
  • What's the URL of the page you're submitting the form on? Commented Jul 20, 2016 at 9:38

3 Answers 3

4

Change the url to the action you have set for the form. That would be:

url = $form.attr('action');
$.ajax({
  type    : 'POST', 
  url     : url,
  data    : formData, 
  dataType: 'json', 
  encode  : true
})
Sign up to request clarification or add additional context in comments.

Comments

4

you can just remove action property from your Form, because you don't use form submit, then change ajax like this:

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

Comments

1

Your form action itself is incorrect:

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

instead of this use:

action="route_path"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.