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
How to send raw text with xmlhttp request?
提问by
I'm trying to send a textarea
field's text to a PHP file using ajax, the text contains HTML characters and should not be encoded.
Using FormData
it works perfectly however it's not supported in ie 9 and older versions! I tried to send the data as string
by setting the requestHeader
to text/plain;charset=UTF-8;
or multipart/form-data
but it didn't work!
the code i'm using is:
我正在尝试textarea
使用 ajax将字段的文本发送到 PHP 文件,该文本包含 HTML 字符,不应进行编码。
使用FormData
它可以完美运行,但是在 ie 9 和更旧的版本中不支持!我试图通过将数据string
设置requestHeader
为text/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 headerRequest
and data
like this:
是的,它可以通过修改headerRequest
并data
像这样完成:
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 FormData
works 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 $data
variable would be the string '<td clas="tdClass">some text<?php echo $contents; ?></td>'
. This is the same as if you'd used the FormData
method.
那么 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));