0

我正在尝试通过 ajax 向 php 页面发送一个长字符串,该页面将处理它并返回我需要的内容,我认为它超出了 GET 容量或类似的东西!但由于某种原因它不起作用

var string = document.getElementById('text').innerHTML; // so long text
var xhr = new XMLHttpRequest();
xhr.open('GET', 'read.php?string=' + string, true);
xhr.send();
xhr.onreadystatechange = function () {
   if (xhr.status == 200 && xhr.readyState == 4) {
    content.innerHTML = xhr.responseText;
   } else {
    content.innerHTML = 'loading';
   }
}

我怎样才能让它工作!

4

2 回答 2

3

只需更换:

xhr.open('GET', 'read.php?string=' + string, true);
xhr.send();

var body = "string=" + encodeURIComponent(string);
xhr.open("POST", "read.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-Length", body.length);
xhr.setRequestHeader("Connection", "close");
xhr.send(body);
于 2013-04-23T02:40:23.677 回答
1

要解决 URL 编码问题,请执行以下操作:

xhr.open('GET', 'read.php?string=' + encodeURIComponent(string), true);
于 2013-04-23T02:37:44.480 回答