JackTheKnife
JackTheKnife

Reputation: 4144

IE not url-encoding single quote/apostrophe

I have a problem with the AJAX call, using jQuery, on IE11 when parameter contains a single quote/apostrophe.

Based on a jQuery documentation https://api.jquery.com/jquery.getjson/

Data that is sent to the server is appended to the URL as a query string. If the value of the data parameter is a plain object, it is converted to a string and url-encoded before it is appended to the URL.

it should to encode any special character but apparently is not doing that right for IE11.

AJAX call looks like:

$.getJSON(
"https://fqdn.to.server:8888/pdqm/endpoint",
{
   firstName: self.firstName(),
   lastName: self.lastName()
},
function (data) { 
    //here is some stuff to do with UI
}

and when using is search for an example Va$$ar O'Connor it will create request URL as

https://fqdn.to.server:8888/pdqm/endpoint?firstName=Va%24%24ar&lastName=O'Connor

when in Chrome/Firefox it is looks like

https://fqdn.to.server:8888/pdqm/endpoint?firstName=Va%24%24ar&lastName=O%27Connor

which is correctly encoded URL.

Any tips how to get that to work on IE? Or another hand - does single quote/apostrophe is a valid character in the query string and must be processed by the endpoint no matter what?

Upvotes: 0

Views: 952

Answers (1)

Drew Higgins
Drew Higgins

Reputation: 71

If this is the only character causing you trouble, you could just hack together a workaround to check if the user is on IE, and replace it from there using the JavaScript replace() function:

var firstName, lastName;
if (window.navigator.userAgent.indexOf("MSIE") > 0) {
    // they are using I.E.
    firstName = self.firstName().replace(/\'/g, "%27");
    lastName = self.lastName().replace(/\'/g, "%27");
}
else {
    // every other browser, just set them normally
    firstName = self.firstName();
    lastName = self.lastName();
}
$.getJSON(
    "https://fqdn.to.server:8888/pdqm/endpoint",
    {
       firstName: firstName,
       lastName: lastName
    },
    function (data) { 
        //here is some stuff to do with UI
    }
);

Upvotes: 1

Related Questions