无法从 openweathermap 获取数据
Not able to get data from openweatherapi
我正在尝试创建一个简单的天气应用程序,我正在使用 jquery Ajax 方法从 openweathermap 检索数据。我正在使用以下方法获取数据。
$(document).ready(function(){
$('#submitWeather').click(function(){
let city = $("#city").val();
if(city != ''){
//Get the AJAX request.
$.ajax({
url:'http://api.openweathermap.org/data/2.5/weather?q=' + city + "&appid=b1b15e88fa797225412429c1c50c122a1",
type: "GET",
//jsonpadded.
dataType: "jsonp",
//the callback for success.
success: function(data){
console.log(data);
}
});
}else {
$("#error").html('Field cannot be empty');
}
});
});
我遇到的问题是它没有得到显示在 console.log 中的数据。这是我在 console.log
中遇到的错误
jquery.min.js:4 GET http://api.openweathermap.org/data/2.5/weather?q=London&appid=b1b15e88fa797225412429c1c50c122a1&callback=jQuery31106677768465103353_1512307813960&_=1512307813961 net::ERR_ABORTED
send @ jquery.min.js:4
ajax @ jquery.min.js:4
(anonymous) @ app.js:6
dispatch @ jquery.min.js:3
q.handle @ jquery.min.js:3
ajax 请求的数据类型是 "json" 而不是 "jsonp"
dataType (default: Intelligent Guess (xml, json, script, or html))
参考:http://api.jquery.com/jquery.ajax/
$.ajax({
url:'http://api.openweathermap.org/data/2.5/weather?q=' + city + "&appid=b1b15e88fa797225412429c1c50c122a1",
type: "GET",
dataType: "json",
success: function(data){
console.log(data);
}
});
更新:
openweathermap 支持 "JSONP" 但你用错了这里是如何将它与 $.ajax 一起使用以调用命名方法的示例
function callback(data){
console.log(data);
}
$.ajax({
url:'http://api.openweathermap.org/data/2.5/weather?q=' + city + "&appid=b1b15e88fa797225412429c1c50c122a1",
type: "GET",
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: "callback"
});
我正在尝试创建一个简单的天气应用程序,我正在使用 jquery Ajax 方法从 openweathermap 检索数据。我正在使用以下方法获取数据。
$(document).ready(function(){
$('#submitWeather').click(function(){
let city = $("#city").val();
if(city != ''){
//Get the AJAX request.
$.ajax({
url:'http://api.openweathermap.org/data/2.5/weather?q=' + city + "&appid=b1b15e88fa797225412429c1c50c122a1",
type: "GET",
//jsonpadded.
dataType: "jsonp",
//the callback for success.
success: function(data){
console.log(data);
}
});
}else {
$("#error").html('Field cannot be empty');
}
});
});
我遇到的问题是它没有得到显示在 console.log 中的数据。这是我在 console.log
中遇到的错误jquery.min.js:4 GET http://api.openweathermap.org/data/2.5/weather?q=London&appid=b1b15e88fa797225412429c1c50c122a1&callback=jQuery31106677768465103353_1512307813960&_=1512307813961 net::ERR_ABORTED
send @ jquery.min.js:4
ajax @ jquery.min.js:4
(anonymous) @ app.js:6
dispatch @ jquery.min.js:3
q.handle @ jquery.min.js:3
ajax 请求的数据类型是 "json" 而不是 "jsonp"
dataType (default: Intelligent Guess (xml, json, script, or html))
参考:http://api.jquery.com/jquery.ajax/
$.ajax({
url:'http://api.openweathermap.org/data/2.5/weather?q=' + city + "&appid=b1b15e88fa797225412429c1c50c122a1",
type: "GET",
dataType: "json",
success: function(data){
console.log(data);
}
});
更新:
openweathermap 支持 "JSONP" 但你用错了这里是如何将它与 $.ajax 一起使用以调用命名方法的示例
function callback(data){
console.log(data);
}
$.ajax({
url:'http://api.openweathermap.org/data/2.5/weather?q=' + city + "&appid=b1b15e88fa797225412429c1c50c122a1",
type: "GET",
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: "callback"
});