2016-12-15 19 views
3

我需要在laravel中执行AJAX请求,并且url是URL :: Action方法和Javascript整数变量之间的串联。URL :: action()和Javascript之间的连接int变量

视图中的javascript函数是

function detailMaintenanceVehicle(vehicleId,placa){ 

    var url = "{{ URL::action('[email protected]',["+vehicleId+"]) }}"; 

    console.log(vehicleId); // This prints integer variables 1,2,3.. 
    console.log(url); // This prints a string http://localhost/.../detailMaintenance/+vehicleId+ 

    $.ajax({ 
     url: url, 
     dataType: 'json', 
     type: 'GET', 
     success: function(data) { 
      ... 
     }, 
     error: function(jqXHR, textStatus, errorThrown) { 
      ... 
     } 
    }); 

} 

级联中使用变量名,它必须使用的值。

//给出:http://localhost/.../detailMaintenance/+vehicleId+

//我需要:http://localhost/.../detailMaintenance/3(例如)

请帮助我。提前致谢!。

回答

5

你可以为尝试:

var url = "{{ URL::action('[email protected]', ['vehicleId' => 'vehicleId']) }}"; 

url.replace("vehicleId", vehicleId); 
+0

它的工作,并在短短一行VAR URL =( “{{...}}”)​​取代(..); –

3

你不应该用PHP生成JS。数据创建隐藏的输入,例如:

{!! Form::hidden('url, action('[email protected])) !!} 

如果你的行动需要ID,只需手动编写部分URL。

然后把它在JS:

var url = $("[name='url']") + vehicleId; 
+0

是的你是对的,在这种情况下更适合我的案例伪代码的答案。同样非常感谢! –

1

您必须创建此方法控制的路径就像: 路线::任何( 'ajaxRequest/{vehicleId}', “DetailMaintenanceController @ getDetailMaintenance”) ;

那么之后你需要在你的JavaScript来改变如下:

function detailMaintenanceVehicle(vehicleId,placa){ 

var url = "{{ URL::to('ajaxRequest/') }}"+vehicleId; //YOUR CHANGES HERE... 

console.log(vehicleId); // This prints integer variables 1,2,3.. 
console.log(url); // This prints a string http://localhost/.../detailMaintenance/+vehicleId+ 

$.ajax({ 
    url: url, 
    dataType: to'json', 
    type: 'GET', 
    success: function(data) { 
     ... 
    }, 
    error: function(jqXHR, textStatus, errorThrown) { 
     ... 
    } 
}); 

} 
+0

Yest,我用它和它的工作,但现在我需要使用URL ::动作。同样谢谢! –