How to prepare encoded POST data on javascript?

前端 未结 5 1093
长发绾君心
长发绾君心 2021-01-04 04:10

I need to send data by POST method.

For example, I have the string \"bla&bla&bla\". I tried using encodeURI and got \"bla&bla&bla\" as t

相关标签:
5条回答
  • 2021-01-04 04:21

    You can also use escape() function.The escape() function encodes a string. This function makes a string portable, so it can be transmitted across any network to any computer that supports ASCII characters.This function encodes special characters, with the exception of: * @ - _ + . /

    var queryStr = "bla&bla&bla";
    alert(queryStr);               //bla&bla&bla
    alert(escape(queryStr));       //bla%26bla%26bla
    

    Use unescape() to decode a string.

    var newQueryStr=escape(queryStr);
    alert(unescape(newQueryStr));   //bla&bla&bla
    

    Note:

        escape() will not encode: @*/+
    
        encodeURI() will not encode: ~!@#$&*()=:/,;?+'
    
        encodeURIComponent() will not encode: ~!*()'
    

    After some search on internet, I got the following:

    escape()

    Don't use it.

    encodeURI()

    Use encodeURI when you want a working URL. Make this call:

    encodeURI("http://www.google.com/a file with spaces.html")
    

    to get:

    http://www.google.com/a%20file%20with%20spaces.html
    

    Don't call encodeURIComponent since it would destroy the URL and return

    http%3A%2F%2Fwww.google.com%2Fa%20file%20with%20spaces.html
    

    encodeURIComponent()

    Use encodeURIComponent when you want to encode a URL parameter.

    param1 = encodeURIComponent("http://xyz.com/?a=12&b=55")

    Then you may create the URL you need:
    
    url = "http://domain.com/?param1=" + param1 + "&param2=99";
    

    And you will get this complete URL:

    http://www.domain.com/?param1=http%3A%2F%2Fxyz.com%2F%Ffa%3D12%26b%3D55¶m2=99
    

    Note that encodeURIComponent does not escape the ' character. A common bug is to use it to create html attributes such as href='MyUrl', which could suffer an injection bug. If you are constructing html from strings, either use " instead of ' for attribute quotes, or add an extra layer of encoding (' can be encoded as %27).

    REF:When are you supposed to use escape instead of encodeURI / encodeURIComponent?

    Also, as you are using JQuery, take a look at this built-in function.

    0 讨论(0)
  • 2021-01-04 04:25

    More recent DOM APIs for URLSearchParams (and via URL, possibly others too) handle encoding in some cases. For example, create or use an existing URL object (like from an anchor tag) I map entries of an object as key value pairs for URL encoded params (to use for GET/POST/etc in application/x-www-form-urlencoded mimetype). Note how the emoji, ampersand and double quotes are encoded without any special handling (copied from the Chrome devtools console):

    var url = new URL(location.pathname, location.origin);
        Object.entries({a:1,b:"                                                                    
    0 讨论(0)
  • 2021-01-04 04:38
    >>> encodeURI("bla&bla&bla")
    
    "bla&bla&bla"
    
    >>> encodeURIComponent("bla&bla&bla")
    
    "bla%26bla%26bla"
    
    0 讨论(0)
  • 2021-01-04 04:40

    I want POST the javascript-created hidden form.

    So the question is if encodeURIComponent() should be used on each POST variable.

    I haven't found the answer for Dmitry's (and my) question in this thread. But I have found the answer in this thread.

    In case of form/POST where you have upload field(s) you must use <form enctype="multipart/form-data">, if no upload field is used, you should choose yourself as described here. Submitting the form should do the job completly, so there is no need to use encodeURIComponent() explicitly.

    If you create a Http POST without using a form or without some library which creates a Http POST from your data, then you need choose an enctype= and join data yourselves.

    This will be easy for application/x-www-form-urlencoded, where you will use encodeURIComponent() on each value and join them exactly as for GET request.

    If you decide use multipart/form-data then ....? You should google more how to encode and join them in such case.

    0 讨论(0)
  • 2021-01-04 04:41

    Use encodeURIComponent() as encodeURI() will not encode: ~!@#$&*()=:/,;?+'

    This has been explained quite well at the following link:

    http://xkr.us/articles/javascript/encode-compare/

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