json请求从cordova到雅虎天气的问题

时间:2015-07-31 08:13:53

标签: json cordova visual-studio-2015 yahoo-api

我正在使用apache cordova创建一个应用程序,我正在使用yahoo weather api,我的问题是它给了我一个错误,底层:

XMLHttpRequest cannot load https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.for…odena%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

另外,我问了使用蜜蜂的密钥,但我不知道把它们放在网址的哪个位置。 有人可以帮帮我吗?我是新手,谢谢

3 个答案:

答案 0 :(得分:0)

我现在不知道你是怎么做的,因为你没有发布任何类型的代码,但我建议你使用jQuery simpleWeather

实施后,您可以使用此代码向yahoos weather API发送请求:

    $.simpleWeather({
        woeid: 'YOURWOEID HERE',
        unit: 'c',
        success: function(weather) {
            var sBasicTemp = weather.temp+'°'+weather.units.temp;
            var sBasicIcon = weather.thumbnail;
            var weatherForecastAll = '<ul class="weatherList">';
            var listClass = '';
            for(var i=0;i<weather.forecast.length;i++) {
                if(i == (weather.forecast.length-1)){listClass = ' class="last"';}
                if(i == 0) {
                    weatherForecastDay = 'Today';
                } else {
                    var forcastDay = weather.forecast[i].day;
                    switch(forcastDay){
                        case('Mon'):
                            weatherForecastDay = 'Monday';
                            break;
                        case('Tue'):
                            weatherForecastDay = 'Tuesday';
                            break;
                        case('Wed'):
                            weatherForecastDay = 'Wednesday';
                            break;
                        case('Thu'):
                            weatherForecastDay = 'Thursday';
                            break;
                        case('Fri'):
                            weatherForecastDay = 'Friday';
                            break;
                        case('Sat'):
                            weatherForecastDay = 'Saturday';
                            break;
                        case('Sun'):
                            weatherForecastDay = 'Sunday';
                            break;
                    }
                }

            }
        }
        error: function(error) {
            $(".weatherBasicTempHead").html('---');
        }

答案 1 :(得分:0)

尝试使用CORS代理:

var link = 'https://linktoapi'; //your link

$.getJSON('http://cors.io/?u=' + link, function(data){ 
    alert(data);
});

答案 2 :(得分:0)

我在使用Cordova和Yahoo Weather API的iOS上遇到了类似的访问问题。事实证明这是Cordova config.xml中所需的Cordova配置访问规则:

<access origin="http://*"/>
<access origin="https://*"/>

当然,这些规则比你想要的要广泛得多,但这是测试这是否是你问题的原因的简单方法。