javascript 如何使用 xmlhttp 请求发送原始文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15847882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:18:43  来源:igfitidea点击:

How to send raw text with xmlhttp request?

javascriptajaxxmlhttprequest

提问by

I'm trying to send a textareafield's text to a PHP file using ajax, the text contains HTML characters and should not be encoded.
Using FormDatait works perfectly however it's not supported in ie 9 and older versions! I tried to send the data as stringby setting the requestHeaderto text/plain;charset=UTF-8;or multipart/form-databut it didn't work! the code i'm using is:

我正在尝试textarea使用 ajax将字段的文本发送到 PHP 文件,该文本包含 HTML 字符,不应进行编码。
使用FormData它可以完美运行,但是在 ie 9 和更旧的版本中不支持!我试图通过将数据string设置requestHeadertext/plain;charset=UTF-8;or来发送数据,multipart/form-data但是没有用!我正在使用的代码是:

var string = '<td clas="tdClass">some text<?php echo $contents; ?></td>';

var data = new FormData();
data.append("data" , string);
var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
xhr.open( 'post', '/path/to/php', true );
xhr.send(data);

what's an alternative way to do this in IE 9?

在 IE 9 中执行此操作的另一种方法是什么?

采纳答案by razz

Yes it can be done by modifying headerRequestand datalike this:

是的,它可以通过修改headerRequestdata像这样完成:

var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");

if(typeof(FormData) == 'undefined'){
    var boundary = '---------------------------' + (new Date).getTime(),//boundary is used to specify the encapsulation boundary of a parameter
        data = "--" + boundary + "\r\n";
        data += 'Content-Disposition: form-data; name="data"\r\n\r\n';//here we specify the name of the parameter name (data) sent to the server which can be retrieved by $_POST['data']
        data += string + "\r\n";
        data += "--" + boundary + "--\r\n";
    xhr.open( 'post', 'writeCode.php', true );
    xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
}else{
    var data = new FormData();
    data.append("data", string);
    xhr.open( 'post', 'writeCode.php', true );
}
xhr.send(data);

回答by Doin

If using FormDataworks correctly for you, then in situations where you can't use it, actually all you need to do is to replace your last line with:

如果 usingFormData适合您,那么在您无法使用它的情况下,实际上您需要做的就是将最后一行替换为:

xhr.send("data="+encodeURIComponent(string));

I think the other answerers were confused by your asking that the text "not be encoded". Form data is generally encoded for sending over HTTP, but then decodedby PHP when it arrives at the server, entirely transparently: you get exactly the original text back, no matter what special characters it might contain. (Assuming you interpret the PHP string as UTF-8).

我认为其他回答者对您要求文本“未编码”感到困惑。表单数据通常被编码用于通过 HTTP 发送,但是当它到达服务器时由 PHP解码完全透明:你得到准确的原始文本,无论它可能包含什么特殊字符。(假设您将 PHP 字符串解释为 UTF-8)。

So, following your example, if your PHP file contained:

因此,按照您的示例,如果您的 PHP 文件包含:

$data = $_POST['data'];

Then the contents of the PHP $datavariable would be the string '<td clas="tdClass">some text<?php echo $contents; ?></td>'. This is the same as if you'd used the FormDatamethod.

那么 PHP$data变量的内容就是 string '<td clas="tdClass">some text<?php echo $contents; ?></td>'。这与您使用该FormData方法的情况相同。

回答by Victor H?ggqvist

Since, well, it appears to be pretty simple after all. Here is some more samples.

因为,好吧,毕竟它看起来很简单。这里还有一些示例。

Just post random text

只需发布随机文本

// just text
var dest = "http://requestb.in/qwb9y3qw";

var xhr = new XMLHttpRequest();
xhr.open("POST", dest, true);

var myText = "foobar";
xhr.send(myText);

Post a JSON object

发布一个 JSON 对象

// post json
var dest = "http://requestb.in/1908jrv1";

var xhr = new XMLHttpRequest();
xhr.open("POST", dest, true);

var myObject = {};
myObject.prop1 = "foo";
myObject.prop2 = "bar";
xhr.send(JSON.stringify(myObject));