通过ajax发送长字符串不起作用 [英] Sending a long string through ajax doesn't work

查看:84
本文介绍了通过ajax发送长字符串不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to send a long string through ajax to php page that will process it and return what I need, I think that It exceeds GET capacity or something like that! but for some reason it doesn't work

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

这篇关于通过ajax发送长字符串不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆