ACP
ACP

Reputation: 35268

XML parsing in jquery doesn't seem to work for me

I have a static html page weather.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    <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...

Upvotes: 0

Views: 376

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039080

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.

Upvotes: 2

Related Questions