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?
Related
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 looked at related questions to to form my ajax request but I can't figure out why this isn't working as the param is always null in the acion
I've done a console.log to check that item in 'data: { data: item }' has a value
//action
public async Task<IActionResult> DeleteMedia(string data)
{
$("#mediaTable").on('click', 'td:nth-child(7)', function () {
var item = $(this).parent().attr("id");
$("#MediaToDownload").val(item);
$.ajax({
url: '#Url.Action("DeleteMedia", "Home")',
type: 'get',
cache: false,
processData: false,
contentType: false,
data: { data: item },
success: function (data) {
location.href = data;
}
});
});
You need to send just the string, as JSON:
data: JSON.stringify(item)
I'm writing an ajax that works when url contains a constant but does not work when url contains a variable because this does not get replaced by the actual value.
$('body').on('click', '.deleteLayer', function () {
var layer_id = $(this).data('id');
confirm("Are You sure want to delete record with layer_id="+layer_id+"?");
$.ajax({
type: "POST",
url: "{{ route('layers.destroy',['layer' => "+layer_id+"])}}",
data: {_method: 'delete', layer:layer_id},
success: function (data) {
table.draw();
},
error: function (data) {
console.log('Error:', data);
}
});
});
});
If I use a value, let's say 50 instead of layer_id then it works!!!:
url: "{{ route('layers.destroy',['layer' => 50])}}",
This is the route that I try to access:
DELETE | admin/layers/{layer} | layers.destroy
If I do not send layer parameter in the url I receive the following error
message : "Missing required parameters for [Route: layers.destroy]
[URI: admin/layers/{layer}]. (View:
/var/www/laravelapp/resources/views/layers.blade.php)"
Why is layer_id, here
url: "{{ route('layers.destroy',['layer' => "+layer_id+"])}}",
not replaced by the actual value?
When you are writing like ['layer' => "+layer_id+"] the js variable is not working. It goes like +layer_id+ as the parameter of the route. You can try like this
var layer_id = $(this).data('id');
var url = '{{ route("layers.destroy", ":id") }}';
url = url.replace(':id', layer_id );
$.ajax({
type: "POST",
url: url,
data: {},
success: function (data) {
},
error: function (data) {
}
});
{{URL::to('/destroy')}}+'/'+layer_id;
Route
Route::get('/destroy/{id}', 'controller#destroy')
Controller
public function destroy($id){
// use $id here
}
Hope you understand.
$(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');
}
});
});
});
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