Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 黑暗天空预测API不返回数据?_Jquery_Ajax_Geocoding_Weather Api - Fatal编程技术网

Jquery 黑暗天空预测API不返回数据?

Jquery 黑暗天空预测API不返回数据?,jquery,ajax,geocoding,weather-api,Jquery,Ajax,Geocoding,Weather Api,我试图在JQuery中显示从Dark Sky Forecast API获取的两个位置的天气信息(其中有地理编码API检索到的lat/lng值)。我设法获得了lat/lng数据,但被预测API拒绝。控制台日志将错误消息显示为: 获取'(x) XMLHttpRequest无法加载“”。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“null”。响应的HTTP状态代码为400 代码如下: //Call Geocoding API to find

我试图在JQuery中显示从Dark Sky Forecast API获取的两个位置的天气信息(其中有地理编码API检索到的lat/lng值)。我设法获得了lat/lng数据,但被预测API拒绝。控制台日志将错误消息显示为:

获取'(x)

XMLHttpRequest无法加载“”。请求的资源上不存在“Access Control Allow 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错误?
提前向您表示感谢,并对格式表示歉意。

这是一个由于跨域GET而导致的一般问题,因为您正在从网页的域外执行XMLHttp请求。如果要快速解决此问题,可以使用联机代理服务(尽管不推荐)。例如,您只需坚持
http://cors.io/?u=
在URL前面

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

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