Javascript Javascript从url读取html到字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8197709/
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
Javascript read html from url into string
提问by Chizpa
I'm currently building a site that should be able to function as a ftp sort of browser. Basically what I have is a ftp server with some images on it.
我目前正在构建一个应该能够用作 ftp 类型浏览器的站点。基本上我拥有的是一个带有一些图像的 ftp 服务器。
What I can't figure out is: if I browse to this ftp site I can view the source of the ftp site (as seen in some browser), what I need is to save that source in a way to a string (using javascript).
我无法弄清楚的是:如果我浏览到这个 ftp 站点,我可以查看 ftp 站点的源代码(如在某些浏览器中所见),我需要的是将该源代码以某种方式保存为字符串(使用 javascript )。
The reason is, that I will make som kind of 'image' browser. I plan on accomplishing that by reading the source into a string, then copy all the image sources and use innerHTML to create a new layout.
原因是,我将制作某种“图像”浏览器。我计划通过将源读入一个字符串来实现这一点,然后复制所有图像源并使用 innerHTML 创建一个新布局。
In short: I want to read information from a url and display it in a different way.
简而言之:我想从 url 读取信息并以不同的方式显示它。
Well, can't seem to get it working. The problem might be that I cannot use serverside scripting. Would it be possible however to put a file on the ftp server that I can load that can dynamically load the data in the same folder? (when I say FTP I actually mean a NAS server with FTP access).
好吧,似乎无法让它工作。问题可能是我无法使用服务器端脚本。但是,是否可以将一个文件放在我可以加载的 ftp 服务器上,该文件可以动态加载同一文件夹中的数据?(当我说 FTP 时,我实际上是指具有 FTP 访问权限的 NAS 服务器)。
回答by Niels
Your answer is Ajax. It can POST and GET data from an URL, just like browsing a website, and it will return the HTML as a string.
您的答案是 Ajax。它可以从 URL POST 和 GET 数据,就像浏览网站一样,它会以字符串形式返回 HTML。
If you plan on using jQuery(real handy), it is easy to use Ajax. Like this example (does not work without the library):
如果您打算使用jQuery(真的很方便),那么使用Ajax很容易。像这个例子(没有库就不能工作):
$.ajax({
url : "/mysite/file.html",
success : function(result){
alert(result);
}
});
If you want to use default Javascript, take a look at http://www.w3schools.com/ajax/default.asp
如果您想使用默认的 Javascript,请查看http://www.w3schools.com/ajax/default.asp
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();
回答by vzwick
There's not much to add to what Niels and rich.okelly have said. AJAX is your way to go.
Niels 和rich.okelly 所说的内容没有什么可补充的。AJAX 是您的最佳选择。
Keep in mind though, that cross-domain restrictions will prohibit you to access data that is not in the same domain. You'll find a possible workaround here.
但请记住,跨域限制将禁止您访问不在同一域中的数据。您会在此处找到可能的解决方法。
回答by Diogo Biazus
As the previous answers stated is possible using HTTP and CORS, but I think you want to take a look at this other thread.
正如前面的答案所述,可以使用 HTTP 和 CORS,但我认为您想看看其他线程。
回答by Mina Samir
IN Javascript to get data without using alert() :
在 Javascript 中获取数据而不使用 alert() :
$.ajax({
url : "/mysite/file.html",
async:false, //this is the trick
success : function(result){
//does any action
}
});