0

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?

Set Kyar Wa Lar
  • 4,488
  • 4
  • 30
  • 57
  • http://stackoverflow.com/questions/8890524/pass-array-to-ajax-request-in-ajax look here, maybe, adding `{...}` for data will help – demo Jan 06 '15 at 09:05
  • send an object like this: `{a:a}`. – Jai Jan 06 '15 at 09:15

2 Answers2

2

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);
        };
    }
});
Arun J
  • 687
  • 4
  • 14
  • 27
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
1

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

Aameer
  • 1,366
  • 1
  • 11
  • 30