views:

23

answers:

1

I have a static html page weather.html

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
        $(document).ready(function() {
            $.ajax({
                type: "GET",
                url: "http://www.google.com/ig/api?weather=Delhi",
                dataType: "xml",
                success: parseXml
            });
        });
        function parseXml(xml) {
           $(xml).find("weather").each(function() {
            alert($(this).attr("temp_c"));
            });
       }
    </script>
</head>
<body>

</body>
</html>

The alert doesn't seem to get displayed in the page when i inspected trough firebug i found this,

XML Parsing Error: no element found Location: moz-nullprincipal:{08ba4230-2feb-48d3-969e-b53579b07b52} Line Number 1, Column 1:
^

also function parseXml doesn't seem to get called...

+1  A: 

You cannot access distant domains using AJAX. See Same Origin Policy. You are trying to access a script located on google.com, so unless this is page is on hosted on this same domain it won't work. The only way to make this work is to setup a proxy server script on your server to which you will send the AJAX call and it will delegate the call to google.com. Another alternative is to use JSONP but the distant script needs to support it.

Darin Dimitrov
@Darin anyother ways of getting it done...
Pandiya Chendur
Yes, either using a server side script that will act as a proxy or JSONP (works only with `GET` requests).
Darin Dimitrov