php - Concatenation between URL::action() and Javascript int variable - Stack Overflow

admin2025-04-18  1

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!.

Share Improve this question asked Dec 15, 2016 at 14:22 olavidpsolavidps 619 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 5

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) {
        ...
    }
});

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744969643a277409.html

最新回复(0)