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.
Thus, you have to replace all ampersands with %26 or use encodeURIComponent(str) in your code.encodeURIComponent('&') gives "%26"
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
If (Key in Object) or If(Object.Hasownproperty(Key)
Keep Bootstrap Dropdown Open When Clicked Off
Catching Errors in JavaScript Promises with a First Level Try ... Catch
React Doesn't Reload Component Data on Route Param Change or Query Change
Detect 64-Bit or 32-Bit Windows from User Agent or JavaScript
How to Find Out with Jquery If an Element Is Being Animated
Check If Element Is Visible on Screen
Difference Between Obtrusive and Unobtrusive JavaScript
How to Parseint a String with Leading 0
JavaScript Get Object Key Name
How to Show a Spinner While Loading an Image via JavaScript
How to Send Email by Using JavaScript or Jquery
Finding "Line-Breaks" in Textarea That Is Word-Wrapping Arabic Text
Regular Expression for Url Validation (In JavaScript)
How Variables Are Allocated Memory in JavaScript
Differencebetween React Native and React