problem when send ajax initiated payment laravel - ajax

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.

Related

Pass a nullable variable from twig to ajax js

I have the following ajax request:
$(document).ready(function () {
$.ajax({
url: '{{ admin.generateUrl('url') }}',
method: 'GET',
data: {
'id': {{ object.id }}
},
success: function (html) {
//
},
});
Data being optional as this request will be called in two different pages, create and /{id}/edit. How do I pass this to my data as the id of the object will not be always present?

Why do I always get undefined value from other data?

I always get undefined values from other data for example: ID:123 the response for this would be the NetAmount: 1000.00 after sending it using AJAX GET REQUEST, so for other data it would just return an undefined value.
MY AJAX
function viewBill(AccountNum){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
method: "GET",
url: "{{ route('view.billing') }}",
data: {AccountNum:AccountNum},
dataType: 'json',
async: true,
beforeSend: function() {$('#userBill').html('Getting Information...')},
success: function(res) {
console.log(res.NetAmount);
$('#checkBill').css('display','block');
$('#userBill').html('₱ '+res.NetAmount);
},
error: function(res) {console.log('Error');},
complete: function() {
}
});
}
My Controller
public function viewBilling(Request $request){
if($request->ajax()){
$viewBilling = Table1::addSelect(['PaymentStatus' => Table2::selectRaw('COUNT(*)')
->whereColumn('AccountNumber','Table1.AccountNumber')
->whereColumn('ServicePeriodEnd','Table1.ServicePeriodEnd')
->where('AccountNumber', $request->AccountNum)
])->where('AccountNumber', $request->AccountNum)
->orderBy('ServicePeriod','desc')
->first();
return Response()->json($viewBilling);
}
}
I tried using dd() and it turns out there are values for the account I tried from the one that returns the NetAmount and from the one that returns an undefined value.
Found the problem, because of the parameter of my viewBill() function, I don't know why would it give me undefined values from other data I did check using the network tools, it just that it wasn't able to send the AccountNumber to the Controller. So I tried removing the parameter. I have an input tag that already has the value of the Account Number so instead of using the above code, I did this
function viewBill(){
var AccountNum = $('#inputAccountNum1').val();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.post({
url: "{{ route('view.billing') }}",
data: {AccountNum:AccountNum},
dataType: 'json',
beforeSend: function() {$('#userBill').html('Getting Information...')},
success: function(res) {
$('#checkBill').css('display','block');
$('#userBill').html('₱ '+res.NetAmount);
},
error: function(res) {console.log('Error');},
complete: function() {
}
});
}

ajax is not working in laravel

$(document).ready(function () {
$('#designation').change(function () {
var opt=this.value;
// var login_url = '{{ url("ajax_crud") }}';
alert(opt);
$.ajax({
//alert(opt);
type: "post",
url:'ajax_type' ,
data: {option:opt},
success: function(data){
alert('hiii');
//$("#div_dprtmnt").html(data);
//document.getElementById("department").innerHTML=data;
}
});
});
});
this is my view page.jquery is working.but inside the ajax alert is not working.how to use $.ajax in laravel.please help me
I think this is an issue with your CSRF token. Ensure you are attaching a token on each request to protect from forgery. You can do all this like this in ajax like this:
(document).ready(function () {
$('#designation').change(function () {
var opt = this.value;
$.ajax({
_token: "{{ csrf_field() }}",
type: "post",
url: "ajax_type",
data: {
option: opt
},
success: function(data){
alert('hiii');
}
});
});
});

Laravel 5.2 | CSRF Token mismatch

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.

Laravel - DELETE Ajax request

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

Resources