【问题标题】:Escape all special characters in a string that is sent by jquery ajax转义 jquery ajax 发送的字符串中的所有特殊字符
【发布时间】:2012-04-25 08:38:46
【问题描述】:

我正在尝试以键值对的形式发送文本,同时将 contentType: "application/json; charset=utf-8", ajax 发布到 Web 服务。我面临的问题是,如果其中一个参数(接受来自用户的文本)有引号(“)它会破坏代码 [错误消息:传入的无效对象]。到目前为止,我已经尝试了这些但没有成功

var text = $("#txtBody").val(); 
var output1 = JSON.stringify(text); 
var output2 = text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 

关于如何为 jquery ajax 帖子转义特殊字符的任何想法?

【问题讨论】:

    标签: jquery text


    【解决方案1】:

    2020 年更新

    使用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"
    

    encodeURI 期望 完整的 URI 也值得一看。

    我之前的建议是使用escape() 方法,但现在它已被弃用,很快也将被删除。

    程序员在编写新的 ECMAScript 代码时不应使用或假设存在这些特性和行为

    更多:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape

    【讨论】:

    • 您的回答对我有用 Starx。但不幸的是,他们不允许我标记多个答案。对不起:(
    • 此函数在 JS 中已被弃用。
    【解决方案2】:

    为什么不使用escape

    escape(text);
    

    https://developer.mozilla.org/en/DOM/window.escape

    编辑!!!!

    如 cmets 中所述,已弃用。

    不推荐使用的 escape() 方法计算一个新字符串,其中某些字符已被十六进制转义序列替换。请改用 encodeURI 或 encodeURIComponent。

    改为使用以下方法之一:

    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

    【讨论】:

    • 我添加了一个答案,因为使用转义方法不是一个好习惯
    • 已弃用 此功能已从 Web 中删除。尽管某些浏览器可能仍然支持它,但它正在被删除。不要在旧项目或新项目中使用它。使用它的页面或 Web 应用程序可能随时中断。
    【解决方案3】:

    对于那些会发现这个问题的人: 请勿使用已从网络上删除的转义方法 请改用encodeURIComponent()encodeURI()
    encodeURIComponent()
    encodeURI()

    【讨论】:

    • 不,永远不要使用encodeURI(),它不可靠并且基于猜测。 encodeURI 根据&= 字符猜测数据名称和值的开始和结束位置,这种猜测可能是错误的。作为一个直接的例子,这个 url 被 encodeURI 错误编码:encodeURI("http://example.org/?foo=i love my mother&father"); - 这里 foo 应该是 i love my mother&father,但是 encodeURI 将它编码为 i love my mother - 同时,encodeURIComponent 进行 0 次猜测,并且在这里正常工作:@987654330 @
    【解决方案4】:

    我遇到了同样的问题,为了解决它,我改变了进行 ajax 调用的方式。

    我有类似的东西

    var datatosend = "Hello+World";
    
    $.ajax({
        "type": "POST", 
        "data": "info=" + datatosend 
    

    然后它在帖子中发送 info=Hello World,将字符 + 替换为空格。

    所以我把它改成正确的json字符串

    $.ajax({
        "type": "POST", 
        "data": {"info":datatosend}, 
    

    现在它可以工作了。 info=Hello+World

    【讨论】:

      【解决方案5】:
      1. 不要使用escape()。来源:MDN escape() Documentation...

      程序员不应使用或假设存在这些功能和行为...

      1. 不要使用encodeURI()(下面列出了替代方案。) 来源:MDN encodeURI() Documentation...

      如果希望遵循更新的 RFC3986 的 URL...以下代码 sn-p 可能会有所帮助:

      function fixedEncodeURI(str) { return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']'); }

      1. 不要使用encodeURIComponent() (下面列出的替代方案。)来源:MDN encodeURIComponent() Documentation...

      为了更严格地遵守 RFC 3986(保留 !、'、(、) 和 *),即使这些字符没有正式的 URI 分隔用途,也可以安全地使用以下字符:

      function fixedEncodeURIComponent(str) { return encodeURIComponent(str).replace(/[!'()*]/g, function(c) { return '%' + c.charCodeAt(0).toString(16); }); }

      因此,如果您想对除这些运算符之外的所有数据进行编码:+@?=:#;,$&,请使用fixedEncodeURI()。如果要包含这些字符(用于定义GET 参数和其他用途),请使用fixedEncodeURIComponent()

      【讨论】:

        猜你喜欢
        • 2011-06-21
        • 2011-05-11
        • 2011-05-07
        • 1970-01-01
        • 2019-07-04
        • 1970-01-01
        • 1970-01-01
        • 2011-02-16
        • 2018-11-08
        相关资源
        最近更新 更多