无法从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');
}
 });
 });

我遇到的问题是,它没有在控制台中获取数据显示。这是我在控制台中遇到的错误

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(默认值:智能猜测(XML,JSON,脚本或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"
        });

最新更新