[英]loading url to bootstrap modal using ajax in Laravel not working
In Laravel -Controller name is ProductController , method is showproductinmodal .在 Laravel -控制器名称是ProductController ,方法是showproductinmodal 。 I tried this, javascript code it worked.
我试过这个,javascript 代码它工作。
Web Route: Web 路线:
Route::get('admin/product/show/{id}', 'Admin\ProductController@showproductinmodal');
JS: JS:
<script>
$('.showinfo').click(function(){
var productid = $(this).data('id');
// AJAX request
$(".modal-body").load("{{URL::to('admin/product/show/')}}"+"/"+productid);
});
</script>
Url loaded and returned some text to modal. Url 已加载并将一些文本返回到模态。 But this Javascript code not worked, i want to use code below:
但是这个 Javascript 代码不起作用,我想使用下面的代码:
$(document).ready(function(){
$('.showinfo').click(function(){
var productid = $(this).data('id');
// AJAX request
$.ajax({
url: '{{route('admin.showproductinmodal')}}',
type: 'post',
data: {id: productid},
success: function(response){
// Add response in Modal body
$('.modal-body').html(response);
}
});
});
});
My web route code我的 web 路线代码
Route::post('admin/product/show/', 'Admin\ProductController@showproductinmodal')->name('admin.showproductinmodal');
My Controller code:我的 Controller 代码:
public function showproductinmodal(Request $id)
{
return "Your test id:" . $id;
}
My a tag我的标签
<a href="#" data-toggle="modal" data-target="#modaldemo3" data-id="15" class="showinfo">Any ID test</a>
Modal works normal, pops up when I use first javascript code everything works ok data loading, but second javascript code is necessary for me.模态工作正常,当我使用第一个 javascript 代码时弹出一切正常数据加载,但第二个 javascript 代码对我来说是必需的。 I inserted alert also in $.ajax request but it didn't work.
我也在 $.ajax 请求中插入了警报,但它没有用。
Might be you are missing crsf token in you case:在您的情况下,您可能缺少 crsf 令牌:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
Set csrf token for ajax call once then call N number of ajax:为 ajax 调用设置 csrf 令牌一次,然后调用 N 个 ajax:
$(document).ready(function () {
$('.showinfo').click(function () {
var productid = $(this).data('id');
let url = "{!! route('admin.showproductinmodal') !!}"
// AJAX request
$.ajax({
url: url,
type: 'post',
data: {
id: productid
},
success: function (response) {
// Add response in Modal body
$('.modal-body').html(response);
}
});
});
});
And it will me more better, if you will use bootstrap model event, and use base url with javascript global variable.如果您使用引导程序 model 事件,并使用基础 url 和 javascript 全局变量,它会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.