2010-07-09 87 views
0

我有一個靜態的HTML頁面weather.htmlXML jQuery的分析似乎並沒有爲我工作

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

警報似乎並沒有得到顯示在頁面的時候,我檢閱槽螢火蟲,我發現這一點,

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

也起到parseXml似乎並沒有得到所謂...

回答

2

您不能訪問使用AJAX遙遠的領域。請參閱Same Origin Policy。您正嘗試訪問位於google.com上的腳本,因此除非此頁面託管在同一個域中,否則它將無法工作。使這項工作的唯一方法是在您的服務器上設置一個代理服務器腳本,您將向其發送AJAX呼叫,並將呼叫委託給google.com。另一種選擇是使用JSONP,但遠處的腳本需要支持它。

+0

完成它的任何其他方式... – 2010-07-09 07:42:48

+0

是的,無論是使用服務器端腳本,將充當代理或JSONP(只與'GET'請求)。 – 2010-07-09 07:48:00