【发布时间】:2020-12-07 11:01:01
【问题描述】:
想通过 AJAX 访问 Show 函数数据,但是当我在路由中传递 id 变量时返回错误
控制器
public function show($id)
{
$features['UnitFeatures'] = UnitFeatures::find($id);
return $features;
}
查看刀片文件
$(document).ready(function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var feature_id = $('#unitFeatures').val();
$.ajax({
url: '{{ route('unitfeatures.show',feature_id) }}', // error in this line when i pass feature_id value to route
type: 'GET',
dataType: 'json',
success: function(response){
console.log(response);
}
});
});
请给我一个解决方案
【问题讨论】:
-
这能回答你的问题吗? stackoverflow.com/questions/29308441/…
-
不,我从输入值中获取一个 id 并传入路由
-
尝试使用键值对在数组中传递 id,类似于
route('unitfeatures.show', ['id' => feature_id]) -
@LQS 不起作用,因为您无法将 JS 变量插入 PHP 代码
标签: javascript php jquery laravel laravel-blade