我已经关注了如何从远程网站解析 XML的一些教程,并在stackoverflow中遇到了精彩明确的问题和答案。 但是,即使在提出问题之后,以下程序也无效。
<!DOCTYPE html>
<html>
<head>
<title>Aviation</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.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).find("City").text();
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>
我想解析的网站是一样的。
此外,我想将其作为独立应用程序开发,即只需与远程网站进行HTML交互。
答案 0 :(得分:1)
如上所述,您可以(需要)使用jsonp,因为faa.gov显然忘记在其API响应中添加适当的标头。 顺便说一下 - 总是喜欢使用Javascript的json而不是jml - 它的使用效果非常好。
// ask faa.gov to add the HTTP header "Access-Control-Allow-Origin: *" to their response
// then you can use this
// jQuery.getJSON('http://services.faa.gov/airport/status/IAD?format=json');
jQuery.ajax({
url: 'http://services.faa.gov/airport/status/IAD?format=json',
dataType: 'jsonp',
success: function (data) {
document.myform.result1.value = data.city;
}
});