Data is getting lost during $.post

2019-08-02 07:35发布

问题:

I have a list <ul> of articles <li> and an event handler on a button.

When clicking the button, I aggregate all <li>s ID (integer) using:

data.articles  = $('#category-articles').sortable('toArray');

// alerts 1298
alert(data.articles.length);

$.post(....);

On the serverside:

<?php
// echoes 968
echo sizeof($_POST['articles']);

Making it clear:

  • trying to send 1298 items in an array data.articles
  • receiving only the first 968 items in an array $_POST['articles']

Data is getting lost during post action. There is no code between the actual post and the target PHP that could filter or remove any items.

I'm using apache and PHP 5.3.

Request:

Content-Length: up to 80,000 bytes

Server:

post_max_size = 100M
upload_max_filesize = 100M

I enabled error reporting but it just shrinks my array and I don't get why it doesn't sent the full data. Anyone has an idea?

回答1:

Duplicate of Array being chopped off over ajax post. Ajax posting limit? ?

Suggests that it's to do with PHP's max_input_vars:
This limit applies only to each nesting level of a multi-dimensional input array.

To solve this without editing the server configuration:

// Serialize the elements into a single var using join():

data.articles  = $('#category-articles').sortable('toArray').join(';');

And on the serverside:

// Unserializing the single variable back into an array:

$articles = explode(';', $_POST['articles']);

The delimiting char ; must not appear inside the elements, chose a different character if it is a problem.