2015-12-25 24 views
2

我试图显示从2个位置(其中有地理编码API检索到的纬度/经度值)的JQuery Dark Sky Forecast API中获取的天气信息。我设法获取纬度/经度数据,但从预测API中被拒绝。控制台日志显示错误信息为:Dark Sky 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”标题。因此不允许原产地'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? 预先感谢并对格式感到抱歉。

回答

2

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

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

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