Set Kyar Wa Lar
Set Kyar Wa Lar

Reputation: 4634

How I can post JavaScript array to Laravel's Controller using jquery

I had an array like the following

var a = [
        {
            "start":"2015-01-12T00:00:00.000Z",
            "allDay":false,
            "promotion_option":"banner"
        },
        {
            "start":"2015-01-13T00:00:00.000Z",
            "allDay":false,
            "promotion_option":"banner"
        }
    ];

And I post that object of array like the following using JQuery Ajax

$.ajax({
    type: method,
    url: url,
    data: a,
    success: function(res) {
        var message = res.mesg;

        if (message) {
            $('.flash').html(message).fadeIn(300).delay(250).fadeOut(300);
        };
    }
});

In my controller when I try dd(Input::all()), it's just return

array(1) {
  ["undefined"]=>
  string(0) ""
}

So, How I can get the value of what I had posted?

Upvotes: 0

Views: 1629

Answers (2)

Rohan Kumar
Rohan Kumar

Reputation: 40639

You need to pass data as Object and use dataType:'json' as you are using res.mesg in success callback like this,

$.ajax({
    type: method,
    url: url,
    data: {a:a},//<== use object here
    dataType:'json',// add this, as you are using res.mesg
    success: function(res) {
        var message = res.mesg;
        if (message) {
            $('.flash').html(message).fadeIn(300).delay(250).fadeOut(300);
        };
    }
});

Upvotes: 2

Aameer
Aameer

Reputation: 1376

try JSON.stringify(a) which will convert it to something like this

"[{"start":"2015-01-12T00:00:00.000Z","allDay":false,"promotion_option":"banner"},{"start":"2015-01-13T00:00:00.000Z","allDay":false,"promotion_option":"banner"}]"

Note that it converts it into a string in your backend whereever you recieve the httprequest you just have to keep that in mind.Hope it helps

Upvotes: 1

Related Questions