我想使用带有 javascript 的 xmlhttprequest 调用 html 在线链接,这是我的代码 但是当代码到达 xmlhttp.open 时它停止了并且不会继续执行
function loadXMLDoc(size,downloadfromurl) {
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var temp = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "http://app.arsel.qa/mobileappspeedtest/samples/256.htm?n=" + Math.random(), false);
xmlhttp.send(null);
}
最佳答案
您正在做的是对该页面的实际 AJAX 请求。 出于安全原因,默认情况下不允许跨域 AJAX 请求。 但是,有多种执行跨域请求的方法,您可以看看 jQuery 是如何做到的,这样您就不必使用纯 JavaScript 重新发明轮子了。 This article应该会有帮助。
无论如何,如果您真的想抓取该页面,有大量用于服务器端脚本语言(如 Java、PHP、Node.js 等)的开源库,它们在收集内容、解析 HTML 等方面非常有用取决于您的需要。
关于javascript从在线链接获取html文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30119679/