【问题标题】:Dark Sky Forecast API does not return data?Dark Sky Forecast API 不返回数据?
【发布时间】:2016-03-31 09:22:30
【问题描述】:

我正在尝试显示从 JQuery 中的 Dark Sky Forecast API 获取的天气信息,用于 2 个位置(其具有通过地理编码 API 检索的纬度/经度值)。我设法获得了 lat/lng 数据,但被 Forecast API 拒绝。控制台日志显示错误消息为:

GET https://api.forecast.io/forecast/260960a77daa4006fe463ae709efed95/[object%20HTMLInputElement],[object%20HTMLInputElement]' (x)

XMLHttpRequest 无法加载 https://api.forecast.io/forecast/260960a77daa4006fe463ae709efed95/[object%20HTMLInputElement],[object%20HTMLInputElement]'。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'null' 不允许访问。响应的 HTTP 状态代码为 400。

代码如下:

  //Call Geocoding API to find lat/lng value
  $.ajax({
    url:"https://maps.googleapis.com/maps/api/geocode/json?address="+fromLocation+"&key=AIzaSyBeUYP6E-zxXWzTzyvuUtUolGLUWy7Jneg",
    type: "GET",
    success: function(res){
      var fromLat = console.log(res.results[0].geometry.location.lat);
      var fromLng = console.log(res.results[0].geometry.location.lng);

      //call next ajax function
      $.ajax({
        url:"https://maps.googleapis.com/maps/api/geocode/json?address="+toLocation+"&key=AIzaSyBeUYP6E-zxXWzTzyvuUtUolGLUWy7Jneg",
        type: "GET",
        success: function(res){
          var toLat = console.log(res.results[0].geometry.location.lat);
          var toLng = console.log(res.results[0].geometry.location.lng);
        }
      });
    }
  });

  //Call Forecast API for weather value
  $.ajax({
    url:'https://api.forecast.io/forecast/260960a77daa4006fe463ae709efed95/'+fromLat+','+fromLng+"'",
    type: "GET",
    success: function(res){
      console.log(res);
    }
  })

是否存在格式错误而不是 API 错误? 提前致谢,对格式表示抱歉。

【问题讨论】:

    标签: jquery ajax geocoding weather-api


    【解决方案1】:

    这是由于跨域 GET 导致的一般问题,因为您正在从网页域外部执行 XMLHttp 请求。如果你想快速解决这个问题,你可以使用在线代理服务(虽然不推荐)。例如,您只需将 http://cors.io/?u= 粘贴在 URL 的前面即可。

    像这样: http://cors.io/?u=http://google.ca

    您还可以查看跨域资源共享 (CORS),它是 W3C 规范。这将是解决这个问题的正确方法。此外,另一种解决方案是去掉 www,因为它可能针对不同的域。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2019-09-10
      • 2017-04-27
      • 1970-01-01
      相关资源
      最近更新 更多