Chris
Chris

Reputation: 14208

jquery ajax get special characters (umlaute)

I query a REST API with strings that contain german umlaute (ä,ö,...)

item.address = "1,Schönberg,5152,Michaelbeuern";
var url = Geo.url + 'geocode?query=' + item.address;
$.get(url,function(results,status) {
     console.log(url);
     console.log(results);
}

The get function returns an empty result, but it works fine when I directly enter the console.log(url) output in my browsers URL bar.

(The ajax request works when the address does not contain any umlaute. That's why I think that this might be the issue.)

Upvotes: 1

Views: 724

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

You need to encode the string you're passing in the querystring. There's a couple of ways to do this. You could use encodeURIComponent():

var url = Geo.url + 'geocode?query=' + encodeURIComponent(item.address);

Alternatively you could provide an object to $.get which jQuery will encode for you:

$.get(Geo.url + 'geocode', { query: item.address }, function(results, status) {
    console.log(results);
});

Personally I would suggest the latter.

Upvotes: 1

Related Questions