Escape all special characters in a string that is sent by jquery ajax

前端 未结 5 617
自闭症患者
自闭症患者 2020-12-14 08:39

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

相关标签:
5条回答
  • 2020-12-14 09:00

    For those who will find this question: Do not use escape method it has been removed from the Web Use encodeURIComponent() or encodeURI() instead
    encodeURIComponent()
    encodeURI()

    0 讨论(0)
  • 2020-12-14 09:06
    1. Do not use escape(). Source: MDN escape() Documentation...

    Programmers should not use or assume the existence of these features and behaviours...

    1. Do not use encodeURI(). (Alternative listed below.) Source: MDN encodeURI() Documentation...

    If one wishes to follow the more recent RFC3986 for URLs... the following code snippet may help:

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

    1. Do not use encodeURIComponent(). (Alternative listed below.) Source: MDN encodeURIComponent() Documentation...

    To be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used:

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

    So, if you want to encode all data except for these operators: +@?=:#;,$&, use fixedEncodeURI(). If you want to include those characters (which are used in defining GET parameters, and other uses), then use fixedEncodeURIComponent().

    0 讨论(0)
  • 2020-12-14 09:10

    2020 Update

    Use encodeURIComponent, an example pulled from 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 which expects a complete URI could be worth a look as well.

    My previous suggestion was to use escape() method, but now that it has been deprecated and soon will be dropped as well.

    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

    0 讨论(0)
  • 2020-12-14 09:11

    I was having the same problem, and to solve it, I change the way I was making the ajax call.

    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.

    So I change it into a correct json string

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

    and now it works. info=Hello+World

    0 讨论(0)
  • 2020-12-14 09:22

    Why not use escape?

    escape(text);
    

    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.

    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/encodeURIComponent

    0 讨论(0)
提交回复
热议问题