user3714932 user3714932 - 1 year ago 124
Ajax Question

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.

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

My ajax call

// process the form
$('.deleteform').submit(function(event) {
var $form = $(this);

var formData = {
topic_id : $form.find('input[name=topic_id]').val()

// process the form
type : 'POST',
url : 'saved',
data : formData,
dataType : 'json',
encode : true

My Controller

public function deleteTopic()

$topic_id = Request::get('topic_id');

$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.

Answer Source

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

url = $form.attr('action');
  type    : 'POST', 
  url     : url,
  data    : formData, 
  dataType: 'json', 
  encode  : true
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download