我正在尝试创建到天气 xml 提要的 ajax 连接,然后解析返回的数据。我在通过 IE 建立连接时没有任何问题,但出于某种原因,我在 FF 或 Safari 中没有任何运气。基本上我正在做的是运行一个包含以下代码的 html 文件。
<html>
<script type="text/javascript" language="javascript">
function makeRequest(zip) {
var url = 'http://rdona.accu-weather.com/widget/rdona/weather-data.asp?location=' + zip;
//var httpRequest;
var httpRequest = false;
if (window.XMLHttpRequest) {
document.write("xmlhttprequest");
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType) {
httpRequest.overrideMimeType('text/xml');
// See note below about this line
}
}
else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
httpRequest.open('GET', url, true);
httpRequest.send('');
}
function alertContents(httpRequest) {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
alert(httpRequest.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
makeRequest(84405);
</script>
</html>
如有任何帮助和/或建议,我们将不胜感激。
最佳答案
恐怕你会因为same origin policy而遇到一些问题,这意味着您不能对另一个域执行 XMLHTTPRequests。
甚至没有jQuery (无论如何你真的应该检查一下)可以帮助你。
关于javascript - 在 IE、FF 和 Safari 中运行 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3724019/