Jquery AJAX not working with session It is not taking old session issue with solution .
In my rails application when I trigger a Ajax request with my
javascript in below way
$j.ajax({
type: "POST",
url:"https://mydomain.com/requests",
data: "requstids="+response.request_ids
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
The request is firing correctly but it is not taking my old session
values of user
It is showing error that your session variable is not available in my
log
any idea why it is not taking previous session ?
and how to fix that ?
I got solution
we need to add below code in your header javascript, which tells use
browser cookie in server externally
###############################################
$(document).ajaxSend(function(e, xhr, options) {
var token = $("meta[name='csrf-token']").attr("content");
xhr.setRequestHeader("X-CSRF-Token", token);
});
################################################
normally by default xhr willnot sent sessionid to server .
javascript in below way
$j.ajax({
type: "POST",
url:"https://mydomain.com/requests",
data: "requstids="+response.request_ids
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
The request is firing correctly but it is not taking my old session
values of user
It is showing error that your session variable is not available in my
log
any idea why it is not taking previous session ?
and how to fix that ?
I got solution
we need to add below code in your header javascript, which tells use
browser cookie in server externally
###############################################
$(document).ajaxSend(function(e, xhr, options) {
var token = $("meta[name='csrf-token']").attr("content");
xhr.setRequestHeader("X-CSRF-Token", token);
});
################################################
normally by default xhr willnot sent sessionid to server .
Comments
Post a Comment