Jquery 请求总是失败

Jquery request always fails

本文关键字:失败 请求 Jquery      更新时间:2023-09-26

我目前正在使用黑暗天空预报 API https://developer.forecast.io/通过 jquery get request 检索 json 对象。所需的 url 参数格式为 (api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE"),而参数的有效格式为:https://api.forecast.io/forecast/02a90a53f4705dc5e5b54f8cda15d805/9.055169,7.49115在浏览器中输入此 URL 将显示一个 JSON 对象。

我尝试的第一件事是jquery get请求:

 $.ajax({
     type: 'GET'
     , data: ''
     , url: "https://api.forecast.io/forecast/02a90a53f4705dc5e5b54f8cda15d805/9.055169,7.49115"
     , success: function (data) {
         alert("works");
     }
     , datatype: 'json'

     , error: function (err) {
         alert("Could not get forecast");
     }
 });

这并不成功 - 它会触发错误功能。 我使用POST请求重试,它也不起作用。请帮助

这是一个简单的 CORS 问题,可以通过使用jsonp数据类型轻松解决:

$.ajax({
  url: "https://api.forecast.io/forecast/02a90a53f4705dc5e5b54f8cda15d805/9.055169,7.49115",
  dataType: "jsonp",
  success: function(data) {
    console.log(data.latitude, data.longitude);
    console.log(data.timezone);
    console.log(data.daily.summary);
  },
  error: function(err) {
    console.log("Could not get forecast");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<em>Loading . . .<em>