0

I am sending some data to my server via $AJAX, now i keep getting the

Token Mismatch Exception Error

.

When i use $http i do not get this error so i do not understand what makes $AJAX soo different. I have checked online for solutions but all the solutions involved doing something like this:

<meta name="csrf-token" content="{{ csrf_token() }}">

Thing is i do not use blade, my client code is completely independent of any server code so using methods like that is not an option, plus i also had a situation in the past where i had to refresh my application after making a post before i could make another post using that method, i dunno why.

What i am basically asking is how to GET CSRF Token from the server using only javascript and SET it in my header in my AJAX request without any server code.

I am using Jquery AJAX for this NOT the $http service in AngularJS

The GET part is the main issue here.

user3718908x100
  • 7,939
  • 15
  • 64
  • 123
  • what ajax request. ...if you aren't using `$http` what are you using? Or is this written incorrectly because it says you have no problem when using `$http`? – charlietfl Jul 24 '15 at 16:38
  • I am using Jquery AJAX – user3718908x100 Jul 24 '15 at 16:39
  • why are you using `$.ajax` when you ask for angular and `$http` already works fine? Did you look at how to set headers in the `$.ajax` docs? – charlietfl Jul 24 '15 at 16:39
  • I am using a directive that uses $.ajax, in my main application i use $.http, i cannot change this, it was written by somebody else. – user3718908x100 Jul 24 '15 at 16:41
  • well there are numerous ways to set the headers all explained in jQuery docs. If you don't want to change the `$.ajax` code use a global method.. start by reading the manual – charlietfl Jul 24 '15 at 16:42
  • I know how to set the headers in $.ajax, my question is how to GET the CSRF token from the server and SET it in $.ajax request. Basically the GETting part is the main issue here. – user3718908x100 Jul 24 '15 at 16:43
  • Please read the last part of my post very well i don't think you understand me. – user3718908x100 Jul 24 '15 at 16:44
  • Was hard to understand because problem description in question is terrible. By default `$http` reads it from cookie....this is explained in `$http` docs. Look at your cookies – charlietfl Jul 24 '15 at 16:47
  • Yes i am aware $http reads it from the cookie by default, however $.ajax appears not to, that is why i am asking HOW to do it. – user3718908x100 Jul 24 '15 at 16:49
  • read the cookie and set the headers from it – charlietfl Jul 24 '15 at 16:49
  • @user3718908, does this help you? https://gist.github.com/alanhamlett/6316427 You need to use fiddler to see what is the cookie name which is being set. – Erti-Chris Eelmaa Jul 24 '15 at 16:50
  • @Erti-ChrisEelmaa principles of beforeSend in gist are good...note however that `$.ajaxSetup` docs state to no longer use that method `we strongly recommend against using this API` – charlietfl Jul 24 '15 at 16:59
  • @Erti-Chris Eelmaa I just tried it, it did't work, so i changed the name of the cookie to match what was in the docs and that still did not work, is there an alternative way? – user3718908x100 Jul 24 '15 at 17:03
  • @user3718908 use that cookie getter function by itself ...even in console to see what it returns. `doesn't work` isn't much of a description...inspect the sent headers too. Also note my comment above about not using `$,ajxSetup` per jQuery docs – charlietfl Jul 24 '15 at 17:09
  • Okay so if it is strongly recommended that we do not use that API then what is the alternative? – user3718908x100 Jul 24 '15 at 17:11

0 Answers0