通过ajax发送长字符串不起作用

Sending a long string through ajax doesn't work

本文关键字:字符串 不起作用 ajax 通过      更新时间:2023-09-26

我试图通过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';
   }
}

我怎样才能使它工作!

直接替换:

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);

要解决url编码问题,请执行:

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