I'm using AJAX post request and sending with CSRF-TOKEN - on my local server worked well, but on my IIS SERV TokenMismatchException in VerifyCsrfToken.php line 67:
This is the code:
$.ajax({
url : '{{ route('dashboard.ajax.update') }}',
method : 'POST',
data : {
table : 'categories',
data : {
order: $count
},
conditions : {
id: $id
}
},
dataType: 'JSON',
headers : {
"X-CSRF-TOKEN": '{{ csrf_token() }}'
}
});
In the console I can see the request with: X-CSRF-TOKEN:w3liodqf8bdOvWH9uVTzLHVVsE0L1uIlCpnOyVVS
What can cause this problem?
$.ajax({
type: 'POST',
url: 'stringUrl',
beforeSend: function (xhr) {
xhr.setRequestHeader('X-CSRF-TOKEN', '{{ csrf_token() }}');
},
data: {
'id': $id // etc..
},
cache: false,
error: function (xhr, type, exception) {
console.log("ajax error response type " + type);
}
});
Try to set it on "beforeSend" event
The problem was in my sessions. I've cleaned all my sessions files, cleaned the cache and used php artisan key:generate. After this - work pretty ok.
Related
I fixed my csrf
I fixed my route , route clear too
but still error show up .
I'm doing ajax form in modal :
My Route
Route::post('increp/store',[\App\Http\Controllers\IncrepController::class,'store'])->name('increp.store');
My ajax
// Create article Ajax request.
$('#submit_increp').click(function(e) {
e.preventDefault();
var data = $("#main-form").serialize();
$.ajax({
url: "{{ route('increp.store') }}",
type: 'POST',
data: data,
dataType: 'json',
beforeSend:function(){
$(document).find('span.error-text').text('');
},
success: function(result) {
if(result.errors) {
console.log(result.errors);
$('.alert-danger').html('');
$.each(result.errors, function(key, val) {
$('span.'+key+'_error').text(val[0]);
});
} else {
$('.alert-danger').hide();
$('.alert-success').show();
}
}
});
});
I doing this for days, i dont have idea how to fix this errors .
Network tab
Console tab
i have this problem when i want send an initiated payment
just work to me (beforeSend) all function not work
and didn't send the data to save transaction
<script>
Moyasar.init({
element: '.mysr-form',
amount: {{ $order->total * 100 }},
currency: 'SAR',
description: 'مادة , رقم الطلب : {{$order->id}} {{ $order->subject->name }}',
publishable_api_key: '{{ config('moyasar.publishable_key') }}',
callback_url: '{{ route('order.checkout', $order->id) }}',
methods: [
'creditcard',
],
on_completed: function (payment) {
$.ajax({
url: '{{route('initialize_transaction')}}',
method: 'POST',
dataType: 'json',
data: {
'order': {{$order->id}},
'payment': payment.id,
'_token': '{{csrf_token()}}',
},
beforeSend : function() {
alert('before')
},
success: function () {
alert('success');
},
fail: function () {
alert('fail')
},
done: function () {
alert('done')
}
})
}
});
</script>
You need to use a double quote instead of a single quote in the URL parameter of ajax. This might solve your issue.
From
url: '{{route('initialize_transaction')}}',
TO
url: "{{route('initialize_transaction')}}",
If you still facing the problem then please check the console and give me error.
I want to pass the unlock key to route using ajax but I am not allowed to do so. The code gives me method not allowed error.
I cant see the error in my code, whether it the route error or some other error
<script>
$(document).ready(function(){
$('.result-container').hide();
$('.unlock-btn').on('click', function(){
$.ajax({
url: "{{route('unlock')}}",
method: 'post',
data: {
theme_id : {{$theme_id}},
key : $('.key').val(),
},
success: function(data){
console.log('success');
if(data === '0')
$('.result-container').show();
else
{
window.location = "{{route('view', ['theme_id' => $theme_id])}}";
}
}
})
});
});
</script>
So your route is a get but in ajax you are sending post request. Change it to post.
Route::post('/unlock', 'ThemeController#unlock')->name('unlock');
And also add a token in data otherwise you will get 419 error for missing CSRF.
$.ajax({
url: "{{route('unlock')}}",
method: 'post',
data: {
theme_id : {{$theme_id}},
key : $('.key').val(),
"_token": "{{ csrf_token() }}",
},
success: function(data){
console.log('success');
if(data === '0')
$('.result-container').show();
else
{
window.location = "{{route('view', ['theme_id' => $theme_id])}}";
}
}
})
$.ajax({
method: 'post',
url: '{{url("admin/append-course-pricebox-div")}}',
data: {_token:{{csrf_token()}},count:boxCount,workshop_id:$("#workshop_id").val()},
cache : false,
contentType: false,
processData: false,
success: function (result) {
if (result.status == 'success') {
$('#success').html('Details are saved successfully.');
$('#success_popup .cd-popup').addClass('is-visible');
}else if(result.status =='blankTerm'){
$("#php-error_includes").show().html(result.message);
}
$('#preLoader').hide();
},
error: function (errors) {
$('#preLoader').hide();
var error = (JSON.parse(errors.responseText));
$.each(error.errors, function (key, val) {
$('#php-error_'+key).html(val);
});
$('.php-error').show();
}
});
when I post request it shows error and Google says it because of csrf error how I pass csrf_token to the controller
please follow this steps
1."_token": "{{ csrf_token() }}", pass csrf token in request
2.<meta name="csrf-token" content="{{ csrf_token() }}">
3.Detailed explaination can be found hereenter link description here
I am using Laravel 5.0. I have created a RESTful Controller. Now i wanna use the destroy function via an ajax call.
This is my JS:
$.ajax({
type: 'POST',
url: '/pv/' + data.id,
data: {_method: 'delete' },
success: function(data){
console.log(data);
}
});
And this is my destroy function:
public function destroy($id)
{
$pv = PV::find($id);
$pv->delete();
return true;
}
All i got is a 500 error.
first check your route in laravel
Route::delete('/deleteprocess', 'Controller#destroy');
in javascript
$.ajax({
url: '/deleteprocess',
type: 'POST',
data:{
'_token': $('meta[name=csrf-token]').attr("content"),
'_method': 'DELETE',
},
success: function(result) {
// Do something with the result
}});
set type : POST,
set token : _token,
set method : _method as DELETE,
That's probabily a "CSRF" Exception. If you are using Ajax Request with jQuery, add this (before your $.ajax):
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
}
});
And, in your "html" ..create a new "meta tag" with csrf-token..value:
{{ csrf_token() }}
Read more: http://laravel.com/docs/5.0/routing#csrf-protection