Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prepare encoded POST data on javascript?

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 the result. I need to replace "&" with something correct for this example.

What kind of method should I call to prepare correct POST data?

UPDATED:

I need to convert only charachters which may broke POST request. Only them.

like image 678
Dmitry Avatar asked Feb 12 '13 10:02

Dmitry


People also ask

How do I encode a value in JavaScript?

In order to encode/decode a string in JavaScript, We are using built-in functions provided by JavaScript. btoa(): This method encodes a string in base-64 and uses the “A-Z”, “a-z”, “0-9”, “+”, “/” and “=” characters to encode the provided string.

Do you need to URLEncode post data?

General Answer. The general answer to your question is that it depends. And you get to decide by specifying what your "Content-Type" is in the HTTP headers. A value of "application/x-www-form-urlencoded" means that your POST body will need to be URL encoded just like a GET parameter string.

How do I encode a space in JavaScript?

URLEncode() function. In JavaScript you can use the encodeURIComponent() function. Click the "URL Encode" button to see how the JavaScript function encodes the text. Note: The JavaScript function encodes space as %20.

How do you decode or encode a URL in JavaScript?

Decoding in Javascript can be achieved using decodeURI function. It takes encodeURIComponent(url) string so it can decode these characters. 2. unescape() function: This function takes a string as a single parameter and uses it to decode that string encoded by the escape() function.


2 Answers

>>> encodeURI("bla&bla&bla")

"bla&bla&bla"

>>> encodeURIComponent("bla&bla&bla")

"bla%26bla%26bla"
like image 199
Álvaro González Avatar answered Oct 21 '22 22:10

Álvaro González


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.

like image 23
Bhushan Firake Avatar answered Oct 21 '22 21:10

Bhushan Firake