Home Ask Login Register

Developers Planet

Your answer is one click away!

Bruno February 2016

jQuery encode blank space in ajax query string

I'm using an API which has blank spaces in the query string.

stationString=KDEN%20KSEA

In my Ajax query i'm using it like this:

var station ="KDEN%20KSEA"
$.ajax({
type: "GET",
url: 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=tafs&requestType=retrieve&format=xml&hoursBeforeNow=3&timeType=issue&
data: {'stationString': station},

My decoded URL in my console looks like this:

stationString:KDEN%2520KSEA

When I set my station variable like this:

var station ="KDEN KSEA"

My decoded URL in my console substitutes the blank space with a "+".

How can I pass a blank space in my string?

Answers


Rory McCrossan February 2016

You need to decode the string before sending it in the request so that it does not get double-encoded by jQuery's $.ajax() method. To do that you can use decodeURIComponent(). Try this:

var station = decodeURIComponent("KDEN%20KSEA");
$.ajax({
    type: "GET",
    url: 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=tafs&requestType=retrieve&format=xml&hoursBeforeNow=3&timeType=issue',
    data: { stationString: station },
    success: function(data) {
        // do something with the returned data...
    }
});

Post Status

Asked in February 2016
Viewed 3,747 times
Voted 10
Answered 1 times

Search




Leave an answer


Quote of the day: live life