使用带有 url 的 javascript 获取 html 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6375461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get html code using javascript with a url
提问by simplified
I am trying to get the source code of html by using a xmlhttprequest with a url. Is there anyone who can help me with this? I am new to programming and I am not too sure how can I do it without jQuery. Thanks in advance.
我试图通过使用带有 url 的 xmlhttprequest 来获取 html 的源代码。有没有人可以帮我解决这个问题?我是编程新手,我不太确定没有 jQuery 该怎么做。提前致谢。
回答by Senad Me?kin
Use jQuery:
使用jQuery:
$.ajax({ url: 'your-url', success: function(data) { alert(data); } });
This data is your HTML.
此数据是您的 HTML。
Without jQuery (just JS):
没有 jQuery(只有 JS):
function makeHttpObject() {
try {return new XMLHttpRequest();}
catch (error) {}
try {return new ActiveXObject("Msxml2.XMLHTTP");}
catch (error) {}
try {return new ActiveXObject("Microsoft.XMLHTTP");}
catch (error) {}
throw new Error("Could not create HTTP request object.");
}
var request = makeHttpObject();
request.open("GET", "your_url", true);
request.send(null);
request.onreadystatechange = function() {
if (request.readyState == 4)
alert(request.responseText);
};
回答by Guy
There is a tutorial on how to use ajax here: https://www.w3schools.com/xml/ajax_intro.asp
这里有一个关于如何使用 ajax 的教程:https: //www.w3schools.com/xml/ajax_intro.asp
This is an example code taken from that tutorial:
这是从该教程中获取的示例代码:
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
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)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
回答by otaxige_aol
For external (cross-site) solution, you can use: https://stackoverflow.com/a/18447625/2657601
对于外部(跨站点)解决方案,您可以使用:https: //stackoverflow.com/a/18447625/2657601
It uses $.ajax()
function, so it includes google jquery.
它使用$.ajax()
函数,所以它包括谷歌 jquery。
回答by Tan
You can use fetchto do that:
您可以使用fetch来做到这一点:
fetch('some_url')
.then(function (response) {
switch (response.status) {
// status "OK"
case 200:
return response.text();
// status "Not Found"
case 404:
throw response;
}
})
.then(function (template) {
console.log(template);
})
.catch(function (response) {
// "Not Found"
console.log(response.statusText);
});
Asynchronous with arrow function version:
与箭头函数版本异步:
(async () => {
var response = await fetch('some_url');
switch (response.status) {
// status "OK"
case 200:
var template = await response.text();
console.log(template);
break;
// status "Not Found"
case 404:
console.log('Not Found');
break;
}
})();