Ajax POST-Request gets cropped

不想你离开。 提交于 2019-12-25 07:26:40

问题


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.


回答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.



来源:https://stackoverflow.com/questions/18217958/ajax-post-request-gets-cropped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!