将 google 天气 api 与 mootools 结合使用

发布于 2024-12-15 00:10:38 字数 1457 浏览 6 评论 0原文

我目前在使用 Mootools 在 JavaScript 中使用 Google 天气 API 时遇到困难。

我正在使用这样的代码:

var location =  $('weather-location').value; 

var req = new Request({
    url: 'http://www.google.com/ig/api?weather=' + location,
    method: 'get',
    onSuccess: function(responseText, responseXML)
    {
        responseXML.getElements('forecast_information').each(function(item)
        {
            item.getElements('city').each(function(city_data)
            {
                $('placename').set('html','Weather for ' + city_data.get('data'));                  
            });
        });           
    }
}).send();   

此代码会导致浏览器错误,在 Firebug 中报告为:

"NetworkError: 405 Method Not Allowed - http://www.google.com/ig/api?weather=72601&location=72601"

从我收集到的信息(我对此相当陌生),此问题是由跨域访问冲突引起的。

我尝试过:

1)使用“post”而不是“get”作为方法...类似的结果

2)使用Request.HTML和Request.JSON而不是Request...类似的结果

3)使用Request.JSONP...不同错误(我认为是文档格式)。我怀疑这是因为 google 天气 API 返回直接 XML 并且未设置为服务 JSONP。

4) 使用 YQL 而不是直接访问 google 的 url(示例 URL:http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D"http%3A%2F%2Fwww .google.com%2Fig%2Fapi%3Fweather%3DDenver%2520CO") ...这没有错误,但也没有返回任何结果(如果输入 URL 则有效到浏览器的地址)。

我能够让它工作的唯一方法是提取天气 XML 服务器端并通过代理将其提供给浏览器。但是,我希望在不增加服务器负担的情况下完成此任务。

这怎么能做到呢?

I am currently having difficulty using the Google weather API in JavaScript using Mootools.

I am using code like this:

var location =  $('weather-location').value; 

var req = new Request({
    url: 'http://www.google.com/ig/api?weather=' + location,
    method: 'get',
    onSuccess: function(responseText, responseXML)
    {
        responseXML.getElements('forecast_information').each(function(item)
        {
            item.getElements('city').each(function(city_data)
            {
                $('placename').set('html','Weather for ' + city_data.get('data'));                  
            });
        });           
    }
}).send();   

This code results in a browser error which is reported in Firebug as:

"NetworkError: 405 Method Not Allowed - http://www.google.com/ig/api?weather=72601&location=72601"

From what I can gather (I am rather new at this), this problem is caused by cross-domain access violation.

I have tried:

1) using 'post' instead of 'get' for method... similar result

2) using Request.HTML and Request.JSON instead of Request... similar result

3) using Request.JSONP ...different error (document formatting I think). I suspect this is because the google weather API returns straight XML and isn't set up to serve JSONP.

4) using YQL instead of a straight url to google (sample URL: http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D"http%3A%2F%2Fwww.google.com%2Fig%2Fapi%3Fweather%3DDenver%2520CO") ... this did not have an error but returned no results either (URL does work if typed into address of browser).

The only way I have been able to get this to work is to pull the weather XML server-side and serve it to the browser via a proxy. However, I am would like to accomplish this without burdening my server.

How can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2024-12-22 00:10:38

这是一个跨域的问题。坚持您拥有的解决方案(代理)。当实际页面位于“yourdomain.com”时,浏览器不允许向“google.com”发出请求。

It is a cross-domain issue. Stick to the solution you have (proxy). The browser is not allowed to do a request to 'google.com' when the actual page is located at 'yourdomain.com'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文