I need to do an AJAX request in laravel, and the url is a concatenation between tne URL::Action method and an Javascript integer variable.
The javascript function in the view is
function detailMaintenanceVehicle(vehicleId,placa){
var url = "{{ URL::action('DetailMaintenanceController@getDetailMaintenance',["+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) {
...
}
});
}
The concatenation uses the variable name, it must use the value.
// Gives : http://localhost/.../detailMaintenance/+vehicleId+
// I need : http://localhost/.../detailMaintenance/3 (for example)
Please help me. Thanks in advance!.
I need to do an AJAX request in laravel, and the url is a concatenation between tne URL::Action method and an Javascript integer variable.
The javascript function in the view is
function detailMaintenanceVehicle(vehicleId,placa){
var url = "{{ URL::action('DetailMaintenanceController@getDetailMaintenance',["+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) {
...
}
});
}
The concatenation uses the variable name, it must use the value.
// Gives : http://localhost/.../detailMaintenance/+vehicleId+
// I need : http://localhost/.../detailMaintenance/3 (for example)
Please help me. Thanks in advance!.
You can try as:
var url = "{{ URL::action('DetailMaintenanceController@getDetailMaintenance', ['vehicleId' => 'vehicleId']) }}";
url.replace("vehicleId", vehicleId);
You should never generate JS with PHP. Create hidden input with data, for example:
{!! Form::hidden('url, action('DetailMaintenanceController@getDetailMaintenance)) !!}
If your action requires ID, just write partial URL manually.
And then get it in JS:
var url = $( "[name='url']" ) + vehicleId;
You must create a route for this method controller just like: Route::any('ajaxRequest/{vehicleId}',"DetailMaintenanceController@getDetailMaintenance");
then after you need to change in your javascript as below:
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) {
...
}
});
}