mardi 15 décembre 2015

POST form data to Controller via AJAX

I have a form and a save button. On click, I want to post all that data from the form to my Controller using Ajax. I could have done that using Input::all(), but I'm trying to avoid my page to refresh.

I want to be able to access those form data, just like input::all(), but using Ajax instead.

How would one go about and implement something like this ?


I've tried

JS

$('.saveRateLimitBTN').click(function (event) {

    var url = '{{env("API_HOST")}}'+'vse/vcpe/'+'{{$cpe_mac}}'+'/vlan/'+'{{env("PVT_VLAN_ID")}}'+'/device/'+'{{$device_mac}}'+'/acl/update';

    var inputs = {};
    $("#editRateLimitForm :input").each(function() {
        inputs[$(this).attr("name")] = $(this).val();
    });

    $.ajax({
        url: url,
        type: 'PUT',
        dataType: 'json',
        data: inputs,
        success: function (data, textStatus, xhr) {
            console.log(data);
        },
        error: function (xhr, textStatus, errorThrown) {
            console.log('PUT error.', xhr, textStatus, errorThrown);
        }
    });

});


DeviceController > updateRate() function

public function updateRate(){

        dd('Here'); // This never get run.
}


Route

Route::put('{cpe_mac}/device/{device_mac}/rate/update',['as'=>'device.update', 'uses'=>'DeviceController@updateRate']);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire