greatgrandpadog
greatgrandpadog

Reputation: 23

use google weather api with mootools

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?

Upvotes: 2

Views: 792

Answers (1)

Torsten Becker
Torsten Becker

Reputation: 4323

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'.

Upvotes: 1

Related Questions