How to Send the "&" (Ampersand) Character via Ajax

How can I send the & (ampersand) character via AJAX?

You can use encodeURIComponent().

It will escape all the characters that cannot occur verbatim in URLs:

var wysiwyg_clean = encodeURIComponent(wysiwyg);

In this example, the ampersand character & will be replaced by the escape sequence %26, which is valid in URLs.

Ampersand (&) character inside a value of jQuery AJAX request data option

Instead of:

data: "id=" + thisId + "&value=" + thisValue

do:

data: { id: thisId, value: thisValue }

This way jquery will take care of properly URL encoding the values. String concatenations are the root of all evil :-)

How to send '&' character in HTTP request body?

As you can read in the attached links you have to encode the ampersand sign.

encodeURIComponent('&') gives "%26"

Thus, you have to replace all ampersands with %26 or use encodeURIComponent(str) in your code.

escaping ampersand in url

How can I send the "&" (ampersand) character via AJAX?

Using an Ampersand in jquery ajax request

You can use the native Javascript "Object" pseudo-class. I don't really understand your code's purpose; but, supossing it is correct, should look like this:

$data = new Object;
$(this).closest("tr").find("input,select,textarea").each(function(){
$data[$(this).attr('name')] = $(this).val();
});

Hope this helps.

URL encode sees “&” (ampersand) as “&” HTML entity

Without seeing your code, it's hard to answer other than a stab in the dark. I would guess that the string you're passing to encodeURIComponent(), which is the correct method to use, is coming from the result of accessing the innerHTML property. The solution is to get the innerText/textContent property value instead:

var str, 
el = document.getElementById("myUrl");

if ("textContent" in el)
str = encodeURIComponent(el.textContent);
else
str = encodeURIComponent(el.innerText);

If that isn't the case, you can usethe replace() method to replace the HTML entity:

encodeURIComponent(str.replace(/&/g, "&"));

Proper way to handle the ampersand character in JSON string send to REST web service

The problem was that I was encoding the whole request string including the key.
I had a request data={JSON} and I was formatting it, but the {JSON} part should only be encoded.

string requestData = "data=" + Uri.EncodeDataString(json) // worked perfect!

Stupid hole to step into.

Passing ampersands in Javascript through AJAX

Some characters have special meaning in URIs, use encodeURIComponent if you want to use them as data.



Related Topics



Leave a reply



Submit