StevieB
StevieB

Reputation: 6533

Jquery ajax whitespace and ampersand issue

I am doing something like this

var apiOptions = {
    url: url,
    dataType: 'jsonp',
    type: "GET",
    success: success
};

if(dataOptions) {
    apiOptions.data = {
        key: self.settings.key,
        limit: self.limit,
        address: dataOptions.address,
    };
}
$.ajax(apiOptions);

And it works fine for everything except for when I have " & ", so spaces around an ampersand. So the request parameter looks like "D+&+D,+enterprisess" or D+%26+D%2C. This then returns a 404 on the server.

Any idea what to do here ?

Upvotes: 13

Views: 1421

Answers (2)

guest271314
guest271314

Reputation: 1

Try using String.prototype.replace() with RegExp() /\s(&)\s/

var data = "123 & abc";
var res = data.replace(/\s(&)\s/, "$1");
console.log("data:", data,"res:", res)

Upvotes: 1

RBoschini
RBoschini

Reputation: 506

For pass parameters with space and speacial character in ajax, you have use escape and unescape function.

var test = 'Exemplae Actão ç @#$%$ ';
var testEscape = escape(test);
console.info(test , testEscape ,  unescape(testEscape ));

This solve you problem

Upvotes: 2

Related Questions