0

I got a ajax request to a method, where I send some data to.

My ajax call is as follows:

$.ajax({
        url: btn.data('url'),
        type: 'POST',
        data: {_method: 'delete', _token: token, items: JSON.stringify(_items)},
        success: function () {
            //
        }
    })

Where token is read from a meta tag and _items is an array with ID's.

When I inspect this ajax call, the form data contains:

_method:delete
_token:15KeBPmbt7WVgm8KNCWG4idqCw3FpGykzREPn8fu
items: ["4","11","15"]

But when I dd(json_decode(Input::get('items') it returns:

0: 3
1: 0
2: 4
3: 1
4: 1
5: 11
6: 2
7: 2
8: 15
9: 2

Edit: when I remove the JSON.stringify the formdata contains:

items[]:1
items[]:2

and returns a array of:

0: 2
1: 0
2: 1
3: 1
4: 1
5: 2
6: 1

Here is my route catch:

Route::delete('user/destroy/multiple', function() {
    dd(Input::get('items'));
});

I really don't know what going wrong...

guidsen
  • 2,333
  • 6
  • 34
  • 55
  • Try dumping the regular `$_POST['items']` and see what that gives you. – Bogdan Oct 29 '14 at 01:14
  • Gives me the same result... I'm setting these values nowhere. And I'm sure it aren't the id's from the items in the database.. – guidsen Oct 29 '14 at 01:16
  • @Bogdan the problem was the point that I forgot to serialize the array to json.. – guidsen Oct 29 '14 at 01:34
  • Only one problem. I can't json_decode the string when I use JSON.stringify.. – guidsen Oct 29 '14 at 01:58
  • You need to set your HTTP headers in your AJAX call try `headers: { 'Content-Type': 'x-www-form-urlencoded' }` –  Oct 29 '14 at 02:50
  • That's not the issue. It has something to do with the json decode or something. Headers are fine, my call does return 200 and form-data is valid json. Only can't read it in php.. – guidsen Oct 29 '14 at 03:05
  • You do not need to make `JSON.stringify`, jquery will make a normal conversion to the items[] = 4, items[] = 11 and so on. You will get an array in php. – Cheery Oct 29 '14 at 03:09
  • post the controller method. – itachi Oct 29 '14 at 03:47
  • here: http://stackoverflow.com/questions/14114690/php-cant-read-array-sent-by-ajax this may solve your problem – Ceeee Oct 29 '14 at 05:36
  • @Cedie It didn't solve my problem ;( – guidsen Oct 29 '14 at 11:51

0 Answers0