0

I have a $.post() that is trying to send a serialized form as the data, but also some additional variables from the page. One of them is the HTML code of the #designer-window div. This HTML can be thousands of characters long, in my example its 3411 characters.

The post request looks like this:

var html = $('#designer-window').html();
var cardid = $('input[name="cardid"]').val();
var id_card = $('input[name="id_card"]').val();

$.post('sv/ajaxadmin/', $('form[name="savecard"]').serialize()+"&html="+html+"&image="+renderedImage+"&cardid="+cardid+"&id_card="+id_card, function(data){
    location.html(original);
    $('form[name="savecard"] button').attr("disabled", false);
    $('form[name="savecard"]').prepend(data);
    console.log(data);
}, "text");

Now, if I use console.log(html); to print the data before it is send. I get the complete HTML string. However in my /sv/ajaxadmin/, if i echo $_POST['html']; then the string is cut off halfway through. The end of the string is missing.

So what I'm guessing is that there is some kind of limit with the amount of characters or bytes you can send using the function above, so the whole string isnt send. Only parts of it.

So a few things:

  • Maybe there is another way to set the post data to be send in the jQuery.post() function? Obviously I can use {data: var, data2: var2...}, the problem with this is combining it with $('form').serialize(). Is it possible?

  • Anyone know if there actually is a limit? I have not found any information regarding a byte/character-limit in jquery post requests. But the only way I can see it, is that there is a limit that is the cause of this issue. Do you have any other ideas?

EDIT: After additional testing I noticed that it seems to have to do with the & character. So in the HTML, if a string contained & that meant that the $.post() data-query-string would get cut off. I tried escaping/replacing & with & but it still mess up the query. So... How the question is then, how do I escape &'s to be able to send them in my query.

SOLVED: I wrapped my html variable with encodeURIComponent(); to escape it. Worked!

Marcus Lind
  • 10,374
  • 7
  • 58
  • 112
  • Possible duplicates: [Is there any size limitation for ajax post?](http://stackoverflow.com/questions/7658193/is-there-any-size-limitation-for-ajax-post) and [Jquery Ajax - post huge string value](http://stackoverflow.com/questions/17810063/jquery-ajax-post-huge-string-value) – jfriend00 Aug 19 '14 at 06:33

2 Answers2

0

The problem may not be the limit of the character count but the limit on how many inputs variables you can send (defaults to 1000).

Adding/Changing this in your php.ini should fix it

max_input_vars 2000

max_input_vars integer

How many input variables may be accepted (limit is applied to $_GET, $_POST and $_COOKIE superglobal separately). Use of this directive mitigates the possibility of denial of service attacks which use hash collisions. If there are more input variables than specified by this directive, an E_WARNING is issued, and further input variables are truncated from the request.

http://php.net/manual/en/info.configuration.php#ini.max-input-vars

Spokey
  • 10,974
  • 2
  • 28
  • 44
  • Thanks for trying to answer, however with additional testing I found out that the problem is related to &-character in my HTML. Any ideas? – Marcus Lind Aug 19 '14 at 06:53
  • Maybe try to replace `&` by `&` in javascript before sending the data – singe3 Aug 19 '14 at 06:56
0

The reason it didnt work was because I didnt escape my var html. The variable was containing hundreds of characters of HTML and if there was an ampersand (&) in there, it meant that my POST string was cut off.

I solved this by using encodeURIComponent() which escapes all the special characters, such as &.

Marcus Lind
  • 10,374
  • 7
  • 58
  • 112