0

I'm using the following code to perform a POST-request:

function sendAjaxRequest(text) {
  var xmlhttp;

  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } else {
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
  }

  xmlhttp.onreadystatechange=function() { 
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      document.getElementById('results').innerHTML = xmlhttp.responseText;
    }
  }

  xmlhttp.open('POST', 'generate.php', true);
  xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xmlhttp.send('code=' + text);
}

Where generate.php looks like this:

<?php

echo isset($_POST['code']) ? $_POST['code'] : '';

The problem now is that if I send long texts they alway get cropped. I tried to increase post_max_size in php.ini but wasn't successful. Btw I'm using XAMPP.

I'd appreciate any guidance.

Mark
  • 77
  • 3
  • How lon is 'long'? What does your text contain? –  Aug 13 '13 at 19:53
  • It's around 9.000 chars and contains of LaTeX code (letters, numbers, `{`, `}`, `[`, `]`, \, `~`, `%`, `?`, `&`). – Mark Aug 13 '13 at 20:01
  • @MikeW I found the problem. It is caused by `&` characters inside `\url{}`. But I don't know why. – Mark Aug 13 '13 at 20:04
  • Try `htmlspecialchars` in output, `echo isset( $_POST['code'] ) ? htmlspecialchars( $_POST['code'] ) : '';` – bystwn22 Aug 13 '13 at 20:06
  • @Mark escape the `&` in your text like this `\&` also see answer below. Its too broad but will help you in the long run. For now escaping should work :). – woofmeow Aug 13 '13 at 20:13
  • I added a link to the comments there .. are you sure you are escaping other special characters too ? Check the link below .. hope it helps @Mark – woofmeow Aug 13 '13 at 20:25

1 Answers1

1

There are generally 3 ways (in the situation you described) in which content can be limited (assuming your AJAX request is correct) :

  1. Maybe your characters are of a different format so you will need to make sure they conform (escape them if needed) to the requirements of the libraries or scripts for them to be displayed in the browser.

    In this case (as @Mark said): You will need to encode the & using encodeURIComponent(yourVar) to output it properly. This will cause it to transform the & to be encoded as %26 and lead to proper output.

  2. PHP is limiting your content - You need to change the post_max_size in php.ini file. But you have already tried it so you should try 2.

  3. Apache server is limiting your content - This you can manipulate with the LimitRequestBody directive. This can be set to a limit from 0 bytes to 2 GB.

woofmeow
  • 2,370
  • 16
  • 13
  • @Mark this may help you with your problem in latex http://tex.stackexchange.com/questions/34580/escape-character-in-latex – woofmeow Aug 13 '13 at 20:24
  • As I'm using the special character inside a `\url` command It's not necessary to escape it because the command does it for you. But thanks for your help. I'll see tomorrow what I can do. – Mark Aug 13 '13 at 20:29
  • Try determining if its getting cut off at a particular length .. then maybe its either 2 or 3 to solve from above. Hope you get to solve would be nice if I could see the part just until it gets cut off. Let me know when you can later :) – woofmeow Aug 13 '13 at 20:32
  • I solved the issue. As mentioned above the problem was caused by the `&` character. Every time I posted an URL containing this character the string got cropped. The solution is to use `encodeURIcomponent()` on the text string or to replace `&` by `%26` by using `text.replace(/&/g, '%26')`. A similar problem has been raised in this question: http://stackoverflow.com/questions/11294107/how-can-i-send-the-ampersand-character-via-ajax. If you add the solution to your answer in order to help future visitors of this site, I will accept it. Thanks for your help! `:)`. – Mark Aug 14 '13 at 06:09
  • 1
    Glad you could solve it @Mark :) ... hope the edit is appropriate. – woofmeow Aug 14 '13 at 08:02