Daniel Oh
Daniel Oh

Reputation: 43

User input not reflected properly in AJAX URL call

Trying to use user's city input (ex. Los Angeles) and include in Ajax URL parameters, however when I console.log(searchURL). It doesn't add a '+' between "los angels" thus breaking the URL. What can be done to make the URL include the + between cities with two words.

var apiKey = "&client_id=OTU3MDMwMHwxNTEwMjUwNDQ0LjI3"
var baseQueryURL = "https://api.seatgeek.com/2/events?" + apiKey;
console.log(baseQueryURL);

function runSearch(queryURL) {
  $.ajax({
      url: queryURL,
      method: 'GET'
    }).done(function(response) {
        console.log(response);
      };

      $("#submitSearch").on("click", function(event) {
        //prevents default event from occuring
        event.preventDefault();

        userCity = $("#userCity").val();
        console.log(userCity);
        //create variable queryCity to hold city queried with URL parameters
        var queryCity = "&venue.city=" + userCity;
        //create searchURL to pass in as queryURL in AJAX call
        searchURL = searchURL + queryCity;
        console.log(searchURL);
        runSearch(searchURL);
      });

Upvotes: 0

Views: 51

Answers (2)

Stephen
Stephen

Reputation: 1

I've used this:

userCity.split(" ").join("+")

Upvotes: -1

David
David

Reputation: 219037

URL-encode the value:

userCity = encodeURIComponent($("#userCity").val());

Upvotes: 2

Related Questions