1

In routes/api.php I have the following route:

Route::post('/session/storeValue', 'HomeController@storeValue');

And in controller I have AJAX function:

<script>

        function noviArtikal() {

  var data = { name: 'User');

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

  $.ajax({
    type: "POST",
    url: '/session/storeValue',
    data: data,
    success: function() {
      console.log("Valueadded");
    }
  });

        };
</script>

But keep getting error page not found when sending AJAX call. What am I doing wrong?

5

4 Answers 4

4

As it is api.php, it automatically adds /api/ to your URL. Try /api/session/storeValue.

From docs:

Routes defined in the routes/api.php file are nested within a route group by the RouteServiceProvider. Within this group, the /api URI prefix is automatically applied so you do not need to manually apply it to every route in the file. You may modify the prefix and other route group options by modifying your RouteServiceProvider class.

EDIT:

add name:

Route::post('/session/storeValue', 'HomeController@storeValue')->name('custom_name');

Then modify your javascript:

$.ajax({ type: "POST", url: '/session/storeValue', data: data,

to

$.ajax({ type: "POST", url: '{{ route('custom_name')}}', data: data,

EDIT 2: And yes, don't send CSRF_TOKEN (check @Yur Gasparyan answer)

Sign up to request clarification or add additional context in comments.

8 Comments

I tried that but then it calls localhost/appname/public/api/session/storeValue with error 404
did you use url: {{ route('yourroutename') }} ?
I'm just calling onclick="noviArtikal()" from button
then don't write url manually, but through route function. laravel.com/docs/5.7/routing#named-routes add name like it's mentioned here. And then use: url: {{ route('yourroutename') }}
But I'm not sure where to call that as I'm just calling js function with Ajax call
|
4

For first you dont need to send CSRF_TOKEN to api. There is no sense to check CSRF-TOKEN because it is an api. Also to send request to api you need to add api prefix manualy.

2 Comments

If I change to Route::post('api/session/storeValue', 'HomeController@storeValue'); and url: 'api/session/storeValue', if you mean like that I get localhost/appname/public/api/session/storeValue called.
You dont need to change ` Route::post('session/storeValue', 'HomeController@storeValue')` to ` Route::post('api/session/storeValue', 'HomeController@storeValue'). You need to move your routes in routes/api.php`
0

add this code in your route/web.php

Route::post('/session/storeValue', 'HomeController@storeValue');

Comments

0

simply give any name to the route just like this:

Route::post('/session/storeValue', 'HomeController@storeValue')->name('somename');

and in your ajax call just change the url attribute to: url: '{{ route('somename')}}',

Laravel attach /api to the api routes automatically through RouteServiceProvider and it is found is /Providers folder.

Also you don't need csrf_token when you are making an Api call.

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.