jeudi 1 septembre 2016

Ajax Request not accessing Controller, Laravel

I have a simple post request that is currently working if I dont specify a controller in my route file. When I change the route to access a controller I have specifically created for AJAX requests it returns a 500 Internal server error.

My Route.php looks like this:

Route::post('ajaxTagStore', 'AjaxController@postAjaxTag');

My Controller looks like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class AjaxController extends Controller
{
public function postAjaxTag(Request $request) {
  if(Request::ajax()){
    return Response::json(Request::all());
  }
}

}

and lastly my AJAX code looks like this:

$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});

$(document).ready(function(){
  $('#tag-btn').click(function(){
    var fname = $('#firstname').val();
    var lname = $('#lastname').val();
    var token = $('#token').val();

    var dataString = $('#tag').serialize();

    $.ajax({
      type: 'POST',
      url: '/ajaxTagStore',
      data: dataString,
      success: function(response){ // What to do if we succeed
        console.log(response);
        $('#postRequestData').html(response);
      }
    })

  });
});

As I said it works if I don't specify a controller and just use this:

Route::post('ajaxTagStore', function(){
 if(Request::ajax()){
  return Response::json(Request::all());
 }
});

Any suggestions as to why this is? (I am using a csrf-token also)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire