mardi 15 décembre 2015

Sent Variable to Controller via Ajax in Blade

I have a variable.


$input that contains this data below

{"_method":"PUT","_token":"rs8iLxwoJHSCj3Cc47jaP5gp8pO5lhGghF1WeDJQ","max_down":null,"max_up":null,"cpe_mac":"000D6766F2F6","device_mac":"503275AE7A69","undefined":""}


I want to sent an it to my controller using Ajax, so I can access it in my controller.

I've tried

$( "form#editRateLimitForm" ).on( "submit", function( event ) {
  event.preventDefault();

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

    var $inputs = JSON.stringify(inputs);

    console.log($inputs);
    //{"_method":"PUT","_token":"rs8iLxwoJHSCj3Cc47jaP5gp8pO5lhGghF1WeDJQ","max_down":null,"max_up":null,"cpe_mac":"000D6766F2F6","device_mac":"503275AE7A69","undefined":""}

    $.ajax({
        url: $url,
        type: 'PUT',
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        data: $inputs ,
        success: function (data, textStatus, xhr) {
            $.gritter.add({
                title: 'Success',
                text: 'Settings successfully changed.',
                class_name: 'growl-success',
                image: '/images/screen.png',
                sticky: false,
                time: ''
            });
            console.log(data);
        },
        error: function (xhr, textStatus, errorThrown) {
            $.gritter.add({
                title: 'Failed',
                text: errorThrown,
                class_name: 'growl-danger',
                image: '/images/screen.png',
                sticky: false,
                time: ''
            });
            console.log('PUT error.', xhr, textStatus, errorThrown);
        }
    });

});


It kept failing on me.

How do I sent that variable to my controller ? Did I do anything wrong ?

Can someone please make it clear, and shed some light on this ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire