我有一个只运行一次的Ajax调用。您必须刷新页面才能再次使用它。
以下是代码:
首先,这是调用函数的click事件:
$("#input-location").click(function (event) {
event.preventDefault();
getWeatherWithUserInput()
.then(function (response) {
weatherCode = response;
console.log(weatherCode);
showMix(weatherCode);
});
});
这是执行Ajax调用的函数。
function getWeatherWithUserInput() {
return new Promise(function (resolve, reject) {
var location = $("#location").val().trim();
var widget = Mixcloud.PlayerWidget(document.getElementById('my-widget-iframe'));
var weatherCSQueryURL = "http://api.openweathermap.org/data/2.5/weather?q = " +
location + " & appid=" + WeatherAPIKey;
$.ajax({
url: weatherCSQueryURL,
method: "GET"
}).done(function (response) {
$(".wind").html("<h1>" + response.name + "</h1>");
resolve(response.weather[0].id);
});
});
};
这是我第二次尝试使用它时没有刷新的错误:
Uncaught (in promise) TypeError: Cannot read property 'contentWindow' of null
at Object.e.Mixcloud.PlayerWidget (widgetApi.js:1)
at app.js:111
at new Promise (<anonymous>)
at getWeatherWithUserInput (app.js:108)
at HTMLButtonElement.<anonymous> (app.js:30)
at HTMLButtonElement.dispatch (jquery-3.2.1.min.js:3)
at HTMLButtonElement.q.handle (jquery-3.2.1.min.js:3)