javascript 获取AngularJS中指定的url的html内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30883995/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:53:03  来源:igfitidea点击:

getting the html content of the url specified in AngularJS

javascripthtmlangularjs

提问by clearScreen

I am using the $http.get('url') to get the content present in the 'url' present. The html in 'url' is as follows

我正在使用 $http.get('url') 来获取存在于 'url' 中的内容。'url'中的html如下

<html>
<head></head>
<body>
    <pre style = "word-wrap: break-word; white-space: pre-wrap;">
        <!-- content is present here -->
    </pre>
</body>
</html>

When I make a $http.get to this url, I get some data and the required content

当我对这个 url 做一个 $http.get 时,我得到了一些数据和所需的内容

GET /TYPE=abvd HTTP 1.1
content required

.0.1234.123 Safari /123.12
Referer: //url of the webpage calling this function
Accept-Encoding: 
...............

How do I get rid of this extra information and receive only the content? (I know we can just parse the response but is there a better way of doing it? )

我如何摆脱这些额外信息并只接收内容?(我知道我们可以只解析响应,但有没有更好的方法呢?)

EDIT: The data response I got was seen in google chrome, when I run the same script in IE10, I get the only the "html content" as desired. Why does this difference occur and how do I make process it?

编辑:我得到的数据响应是在谷歌浏览器中看到的,当我在 IE10 中运行相同的脚本时,我只得到了所需的“html 内容”。为什么会出现这种差异,我该如何处理?

回答by Drakes

$http.getreturns an HttpPromise, and from it you can get the underlying data like so:

$http.get返回一个HttpPromise,您可以从中获取基础数据,如下所示:

$http.get('url').then(function(response) {
    html = response.data;
});

To get even more useful data, you can expand this like so:

要获得更多有用的数据,您可以像这样扩展它:

$http.get('url').then(
   // success handler
   function(response) {
       var data = response.data,
           status = response.status,
           header = response.header,
           config = response.config;
   }, 
   // error handler
   function(response) {
       var data = response.data,
           status = response.status,
           header = response.header,
           config = response.config;
   });

Demo:JSBin

演示:JSBin

Edit:If there are still HTML issues, you can look into $sce.trustAsHtml(html)or PhantomJS with references:

编辑:如果仍然存在 HTML 问题,您可以查看$sce.trustAsHtml(html)或 PhantomJS 参考: