gpt4 book ai didi

javascript - XML解析远程服务器

转载 作者:行者123 更新时间:2023-12-03 11:34:46 24 4
gpt4 key购买 nike

我学习了一些关于如何从远程网站解析 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>

我尝试解析的网站是相同的。

US FAA

我还想将其开发为独立的应用程序,即仅与远程网站交互的 HTML。

最佳答案

如上所述,您可以(需要)使用 jsonp,因为 faa.gov 显然忘记在其 API 响应中添加适当的 header 。顺便说一句 - 总是更喜欢 json 而不是使用 Javascript 的 xml - 它使用起来要好得多。

// 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;
}
});

关于javascript - XML解析远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26567848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com