我想解析來自遠程網站的xml數據http://services.faa.gov/airport/status/IAD?format=xml ...但我無法解析xml數據,我只是得到錯誤。但是我能夠解析來自同一個遠程網站http://services.faa.gov/airport/status/IAD?format=json的JSON數據。我已經使用解析XML數據的代碼是:從遠程網站解析xml數據
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Aviation</title>
<script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
var result;
function xmlparser() {
$.ajax({
type: "GET",
url: "http://services.faa.gov/airport/status/IAD?format=xml",
dataType: "xml",
success: function (xml) {
result = xml.city;
document.myform.result1.value = result;
},
error: function (xml) {
alert(xml.status + ' ' + xml.statusText);
}
});
}
</script>
</head>
<body>
<p id="details"></p>
<form name="myform">
<input type="button" name="clickme" value="Click here to show the city name" onclick=xmlparser() />
<input type="text" name="result1" readonly="true"/>
</form>
</body>
</html>
因爲我已打印錯誤消息我只是得到的錯誤作爲警告框「o錯誤」。任何人請幫助解析來自遠程網站的XML數據。 注意:我也有,而不是「城市」,但它不工作... 在此先感謝...
阿賈克斯不能做跨域請求。您將不得不使用服務器端腳本來代理信息 – 2012-08-02 13:09:51
如果返回的響應是jsonp,則JavaScript可以通過Ajax執行跨域請求。當談到xml和json時,你不能從JavaScript做跨域請求,並且@Pekka評論說你需要有一個服務器端代理來發送請求。 – JustinMichaels 2012-08-02 13:16:08
@Pekka:請你解釋一下如何用這個例子來做?但是,我怎麼能夠在使用JSON數據的Ajax中跨域請求?謝謝你... – 2012-08-02 13:17:17