Ajax Post and Plus Sign ( + ) -- How to Encode

AJAX POST and Plus Sign ( + ) -- How to Encode?

Use encodeURIComponent() in JS and in PHP you should receive the correct values.

Note: When you access $_GET, $_POST or $_REQUEST in PHP, you are retrieving values that have already been decoded.

Example:

In your JS:

// url encode your string
var string = encodeURIComponent('+'); // "%2B"
// send it to your server
window.location = 'http://example.com/?string='+string; // http://example.com/?string=%2B

On your server:

echo $_GET['string']; // "+"

It is only the raw HTTP request that contains the url encoded data.

For a GET request you can retrieve this from the URI. $_SERVER['REQUEST_URI'] or $_SERVER['QUERY_STRING']. For a urlencoded POST, file_get_contents('php://stdin')

NB:

decode() only works for single byte encoded characters. It will not work for the full UTF-8 range.

eg:

text = "\u0100"; // Ā
// incorrect
escape(text); // %u0100
// correct
encodeURIComponent(text); // "%C4%80"

Note: "%C4%80" is equivalent to: escape('\xc4\x80')

Which is the byte sequence (\xc4\x80) that represents Ā in UTF-8. So if you use encodeURIComponent() your server side must know that it is receiving UTF-8. Otherwise PHP will mangle the encoding.

AJAX POST and Plus ( + ) and (&) Sign How to Encode?

need to encode your params encodeURIComponent
Note:-When you access $_GET, $_POST or $_REQUEST in PHP, you are retrieving values that have already been decoded.

URL Encode a string in jQuery for an AJAX request

Try encodeURIComponent.

Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).

Example:

var encoded = encodeURIComponent(str);

ajax - Why jquery replaces + with a space ( )?

In some GET and POST requests (most likely in the URL, or via a form), spaces are encoded as "+" (plus) symbols before they are passed to the server. You can see this behaviour if you do a normal GET request - you will see something like google.com?q=test+example If you want to pass a plus symbol via an ajax GET/POST request, you need to "urlencode" it. The URL encoded value for + is %2B.

Also note:

The javascript encodeURIComponent() function can be used, as answered in:

AJAX POST and Plus Sign ( + ) -- How to Encode?

why replaced plus in string by space

When using $.ajax, use an object rather than a string with the data: option. Then jQuery will URL-encode it properly:

data: { data: data },

If you really want to pass a string, you should use encodeURIComponent on any values that might contain special characters:

data: 'data=' + encodeURIComponent(data),

Send plus of minus symbol as a post value using AJAX

The problem is that you need to encode your value for use in a url:

data:"BloodGroup="+$("#bloodGroup").val(),

If you don't encode the value, a + will be come a space when the value is decoded on the server.

You can have jQuery do that automatically:

data: { 'BloodGroup': $("#bloodGroup").val() },

If you want to encode it manually (when you are not using jQuery for example), you can use:

data: "BloodGroup=" + encodeURIComponent($("#bloodGroup").val()),

Ajax POST method converts my + value in a string to why?

Use the encodeURIComponent() function to turn your data in valid encoded data for the request:

xhr.open("POST", url, true);
xhr.send(encodeURIComponent(postdata));

Unable to send '+' through AJAX post?

Use

request.send("sal="+encodeURIComponent(sal));

The + is interpreted as an space on the server side so you need to encode the string first.

More info here:

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

AJAX changes content of the string being sent

You have to change it like this (use encodeURIComponent() on each element):

xmlhttp.open("POST", "/note.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
console.log(" >>>> " + note + " " + noteHTML);
xmlhttp.send("what=edit¬e=" + encodeURIComponent(note) + "¬eHTML=" + encodeURIComponent(noteHTML) + "¬eIdx=" + encodeURIComponent(noteIdx));

https://stackoverflow.com/a/4276396/4916265

Jquery ajax call with '+' sign

azione = escape(String(azione));

should be

azione = encodeURIComponent(String(azione));

or simply

azione = encodeURIComponent(azione);


Related Topics



Leave a reply



Submit