Reputation: 31
I have an Ajax function which calls a laravel route and which I need to pass a variable, I have done some things but I didn't spoil it by doing well, let's see if you can help me.
fragment controller where I pass the variable to the view
'presupuesto_id' => $presupuesto->id,
Function Ajax
$("#editar_presupuesto").click(function(){
var url ="{!!route('presupuesto.update',$presupuesto_id)!!}";
$.ajax({
type: "POST",
url: url, // This is what I have updated
data: $("#form_presupuesto").serialize(),
success:function(data) {
}
if (data.errors != "" && data.errors != null) {
}
}
})
});
Upvotes: 0
Views: 43
Reputation: 1527
use data attribute
your html will be look like this..
<button type="button" id="presupuesto_id" data-route="{{ route('presupuesto.update',$presupuesto_id)}}">Submit</button>
your js will be look like this..
$("#presupuesto_id").click(function(){
let url = $(this).data('route');
$.ajax({
type: "POST",
url: url, // This is what I have updated
data: $("#form_presupuesto").serialize(),
})
});
Upvotes: 1