转义 jquery ajax 发送的字符串中的所有特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10133082/
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
Escape all special characters in a string that is sent by jquery ajax
提问by developer747
I am trying to send text in key value pairs while doing a contentType: "application/json; charset=utf-8",
ajax post to a web service. The problem I am facing is that if one of the parameters (that accepts text from the user) has quotes (") it breaks the code [Eror message: Invalid object passed in ] . So far I have tried these without any success
我正在尝试在向contentType: "application/json; charset=utf-8",
Web 服务执行ajax 发布时以键值对的形式发送文本 。我面临的问题是,如果其中一个参数(接受来自用户的文本)有引号(“),它会破坏代码 [错误消息:传入的对象无效]。到目前为止,我已经尝试了这些但没有任何成功
var text = $("#txtBody").val();
var output1 = JSON.stringify(text);
var output2 = text.replace(/[-[\]{}()*+?.,\^$|#\s]/g, "\$&");
Any ideas on how to escape the special characters for the jquery ajax post?
关于如何转义 jquery ajax 帖子的特殊字符的任何想法?
回答by Trevor
Why not use escape
?
为什么不使用escape
?
escape(text);
https://developer.mozilla.org/en/DOM/window.escape
https://developer.mozilla.org/en/DOM/window.escape
EDIT!!!!
编辑!!!!
As mentioned in comments, this is deprecated.
正如评论中提到的,这已被弃用。
The deprecated escape() method computes a new string in which certain characters have been replaced by a hexadecimal escape sequence. Use encodeURI or encodeURIComponent instead.
不推荐使用的 escape() 方法计算一个新字符串,其中某些字符已被十六进制转义序列替换。请改用 encodeURI 或 encodeURIComponent。
Instead use one of the following:
而是使用以下方法之一:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
回答by zaynetro
For those who will find this question:
Do not use escape method it has been removed from the WebUse encodeURIComponent()
or encodeURI()
instead
encodeURIComponent()
encodeURI()
对于那些会发现这个问题的人:
不要使用转义方法,它已从 Web使用中删除,encodeURIComponent()
或者encodeURI()
改为
encodeURIComponent()
encodeURI()
回答by David
I was having the same problem, and to solve it, I change the way I was making the ajax call.
我遇到了同样的问题,为了解决它,我改变了进行 ajax 调用的方式。
I had something like
我有类似的东西
var datatosend = "Hello+World";
$.ajax({
"type": "POST",
"data": "info=" + datatosend
And it send on the post info=Hello World, replacing the character + with a blank space.
并且它在 post info=Hello World 上发送,用空格替换字符 +。
So I change it into a correct json string
所以我把它改成正确的json字符串
$.ajax({
"type": "POST",
"data": {"info":datatosend},
and now it works. info=Hello+World
现在它起作用了。信息=你好+世界
回答by Starx
2020 Update
2020 更新
Use encodeURIComponent, an example pulled from MDN.
使用encodeURIComponent,一个从 MDN 中提取的示例。
// encodes characters such as ?,=,/,&,:
console.log(encodeURIComponent('?x=шеллы'));
// expected output: "%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"
console.log(encodeURIComponent('?x=test'));
// expected output: "%3Fx%3Dtest"
encodeURIwhich expects a complete URIcould be worth a look as well.
需要完整 URI 的encodeURI也值得一看。
My previous suggestion was to use escape()
method, but now that it has been deprecated and soon will be dropped as well.
我之前的建议是使用escape()
方法,但现在它已被弃用,很快也会被删除。
Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code
More: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape
程序员在编写新的 ECMAScript 代码时不应使用或假设这些特性和行为的存在
更多:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape