lundi 5 décembre 2016

Laravel 5.3 Ajax Request 405 error

This is the first time I am trying AJAX and the API routes file within laravel 5.3. I am currently just trying to make a simple AJAX call to make sure I can get everything working and know how to continue in future. However, I keep getting 405 errors and I am not sure what I am doing wrong. This is what i have currently: My api.php route folder:

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::group(['middleware' => ['auth:api']], function () {
Route::post('/test', function (Request $request) {
  return response()->json(['name' => 'test']);
});
});

And my ajax call:

<script src="http://ift.tt/2dletCP"
  integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="
  crossorigin="anonymous"></script>
<meta name="csrf-token" content="" />
<script type="text/javascript">
                var CSRF_TOKEN = jQuery('meta[name="csrf-token"]').attr('content');

      jQuery.ajax({
           url: '/api/test/',
           type: 'POST',
           data: {_token: CSRF_TOKEN},
           dataType: 'JSON',
           success: function (data) {
           console.log(data);
           }
        });
    </script>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire