[英]how to send a variable for route introduced in ajax in laravel
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.我有一个 Ajax 函数,它调用 laravel 路由,我需要传递一个变量,我做了一些事情,但我并没有因为做得好而破坏它,让我们看看你是否可以帮助我。
fragment controller where I pass the variable to the view我将变量传递给视图的片段控制器
'presupuesto_id' => $presupuesto->id,
Function Ajax函数 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) {
}
}
})
});
use data attribute使用数据属性
your html will be look like this..你的 html 看起来像这样..
<button type="button" id="presupuesto_id" data-route="{{ route('presupuesto.update',$presupuesto_id)}}">Submit</button>
your js will be look like this..你的js看起来像这样..
$("#presupuesto_id").click(function(){
let url = $(this).data('route');
$.ajax({
type: "POST",
url: url, // This is what I have updated
data: $("#form_presupuesto").serialize(),
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.