gpt4 book ai didi

javascript - 用户输入未正确反射(reflect)在 AJAX URL 调用中

转载 作者:行者123 更新时间:2023-12-03 03:03:24 26 4
gpt4 key购买 nike

尝试使用用户的城市输入(例如洛杉矶)并包含在 Ajax URL 参数中,但是当我 console.log(searchURL) 时。它不会在 "losangels" 之间添加 '+',从而破坏 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);
});

最佳答案

URL-encode值:

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

关于javascript - 用户输入未正确反射(reflect)在 AJAX URL 调用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47275524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com