0

I'm trying to make a post request via jquery but It seems to be something wrong.

Route:

Route::post('fblogin','UserController@fblogin')->name('fblogin');

Jquery:

$.post( "{{route('fblogin')}}", { 'response': response, '_token':'{{ csrf_token() }}' }, function( data ) {
      alert( "Data Loaded: " + data );
});

Controller:

public function fblogin(Reqeust $request)
    {           
            var_dump($request->response);
    }
medo ampir
  • 1,850
  • 7
  • 33
  • 57

2 Answers2

0

I you are in a js file you cannot use blade's mustache syntax {{ }}.

But if you want you can set the 'X-CSRF-TOKEN' header globaly to jQuery

let token = document.head.querySelector('meta[name="csrf-token"]')

jQuery.ajaxSetup({
  headers : {
    'X-CSRF-TOKEN' : token.content
  }
});

$.post(window.location.origin + '/fblogin', { 'response': response, '_token': token.content }, function( data ) {
      alert( "Data Loaded: " + data );
});

And if you want to generate your route dynamically there are several way to do that, among which this package

0

oh my, it is a typewriting issue:

check the controller first line, it is "Reqeust $request" but it should be "Request $request"

I'm not sure why doesn't it returned an error or anything.

medo ampir
  • 1,850
  • 7
  • 33
  • 57
  • Because it doesn't get that far. See the comments, the URL is not being generated in the javascript. – aynber Mar 22 '19 at 14:13
  • I didn't understand the question the first time. I thought that you are asking about the jquery.min.js file. But the jquery request code is in a blade php file. Sorry for the misunderstanding – medo ampir Mar 22 '19 at 14:52