Laravel 5.4-在javascript函数中使用控制器路由

Laravel 5.4-在javascript函数中使用控制器路由,javascript,laravel-5,datatables,Javascript,Laravel 5,Datatables,我有一个表(datatables)和一个编辑图标,我想用参数调用路由 我的功能目前是 function editItem(id) { //alert(id); } 我想做的是打电话 UsersController@editItem 并将id传递给控制器 我有点卡住了 您混淆了服务器端逻辑和客户端逻辑。要调用的控制器方法是客户端逻辑中不存在的服务器端组件 您应该将路由定义传递给JS,并使用AJAX调用此url(jquery或axios是您的朋友),并以异步方式从

我有一个表(datatables)和一个编辑图标,我想用参数调用路由

我的功能目前是

function editItem(id)
    {
        //alert(id);
    }
我想做的是打电话

UsersController@editItem
并将id传递给控制器


我有点卡住了

您混淆了服务器端逻辑和客户端逻辑。要调用的控制器方法是客户端逻辑中不存在的服务器端组件

您应该将路由定义传递给JS,并使用AJAX调用此url(jquery或axios是您的朋友),并以异步方式从服务器读取响应

我尝试用一些示例代码来解释:

//routes.php
Route::get('/edit', [
  'uses' => 'UsersController@editItem',
  'as' => 'edit-item'
]);

//blade file
<script>
window.routes = {
  editItem: "{{ route('edit-item') }}",
}
function editItem(id)
{
  $.post(window.routes.editItem, { id: id }).then(function(response) {
    console.log(response);
  });
}    
</script>
//routes.php
路由::获取(“/edit”[
'使用'=>'UsersController@editItem',
'作为'=>'编辑项'
]);
//刃锉
window.routes={
editItem:“{{route('edit-item')}}”,
}
功能编辑项(id)
{
$.post(window.routes.editItem,{id:id}).then(函数(响应){
控制台日志(响应);
});
}    

您混淆了服务器端逻辑和客户端逻辑。要调用的控制器方法是客户端逻辑中不存在的服务器端组件

您应该将路由定义传递给JS,并使用AJAX调用此url(jquery或axios是您的朋友),并以异步方式从服务器读取响应

我尝试用一些示例代码来解释:

//routes.php
Route::get('/edit', [
  'uses' => 'UsersController@editItem',
  'as' => 'edit-item'
]);

//blade file
<script>
window.routes = {
  editItem: "{{ route('edit-item') }}",
}
function editItem(id)
{
  $.post(window.routes.editItem, { id: id }).then(function(response) {
    console.log(response);
  });
}    
</script>
//routes.php
路由::获取(“/edit”[
'使用'=>'UsersController@editItem',
'作为'=>'编辑项'
]);
//刃锉
window.routes={
editItem:“{{route('edit-item')}}”,
}
功能编辑项(id)
{
$.post(window.routes.editItem,{id:id}).then(函数(响应){
控制台日志(响应);
});
}    

我有点困惑,因为我希望将用户带到编辑页面,该页面将在控制器功能中定义。因此,您需要将用户重定向到路由。您应该将路径从PHP传递到Javascript,然后再传递到
document.location=window.routes.editItem
您能给我一些示例代码吗。我真的很感谢你的帮助。我有点困惑,因为我希望将用户带到编辑页面,该页面将在控制器功能中定义。因此,你需要将用户重定向到路由。您应该将路径从PHP传递到Javascript,然后再传递到
document.location=window.routes.editItem
您能给我一些示例代码吗。我真的很感谢你的帮助。