I have page that POSTS AJAX to the server, and often the data will be Unicode.
This works most the time, but it appears that sometimes it’s breaking and I’m can’t seem to find the answer anywhere. I have been using encodeURI, but I’m not sure that’s correct.
Here’s a example of the code:
$.ajax({
type: "POST",
url: "SomePage.php",
data: "val1=" + encodeURI(unicodeVariable) + "&presentation=" + encodeURI(someUnicodeVariable),
success: function(msg){
}
});
I’m guessing what’s happening is sometimes a Unicode character includes an & that’s breaking it.
What’s the correct way to encode Unicode for an AJAX call with JQuery?
Thanks.
Here’s a good link that compares all of the encoding methods.
encodeURIComponent() encodes also the “&” symbol:
Or you can just convert this line:
into an object:
And jQuery will automatically make sure the data is encoded, using encodeURIComponent().