尝试使用用户的城市输入(例如,洛杉矶)并包括在Ajax URL参数中,但是当我console.log(searchURL)
时。它不会在'+'
之间添加"los angels"
从而破坏URL。要使URL在包含两个单词的城市之间包含+
,可以做些什么。
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);
});
最佳答案
userCity = encodeURIComponent($("#userCity").val());