Answer the question
In order to leave comments, you need to log in
How to send ajax post with csrf?
Here is the code, please give me the cities:
if ($('select').is('#city')){
$.post('/city/index', {_csrf: 'USER_CSRF_TOKEN'})
.done(function(resp){
$('#city').empty();
_each(resp.cities, city)
{
$('#city').append($('<option value="' + city.id + '"> city.name</option>'));
}
})
.fail(function() {
alert("error");
});
}
$.post('/city/index', {_csrf: 'USER_CSRF_TOKEN'})
Answer the question
In order to leave comments, you need to log in
coped. in my case the solution is this:
var csrf;
$.get('/csrfToken').done(function (resp) {
csrf = resp._csrf;
});
setTimeout(function(){
if ($('select').is('#city')){
console.log(csrf);
$.post('/city/index', {_csrf: csrf })
.done(function(resp){
$('#city').empty();
console.log(resp.cities);
$.each(resp.cities, function(i, city){
$('#city').append('<option value="' + city.id + '"> ' +city.name + '</option>');
});
})
.fail(function() {
alert("error");
});
}
}, 0
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question