从另一个域上托管的另一个页面的正文标签获取文本-javascript/jQuery [英] Get text from body tag of another page hosted on another domain - javascript / Jquery

查看:64
本文介绍了从另一个域上托管的另一个页面的正文标签获取文本-javascript/jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设在服务器上显示了 www.example.com/some.html 网页.现在,我想从另一个域获取该页面的body标签内的内容.我该怎么做?我试图找到解决方案,但是还没有运气.

Suppose a webpage www.example.com/some.html hosed on server. Now I want to get the content inside body tag of that page from another domain. How can I do it? I tried to find solution, but no luck with that yet.

以下是我思考和发现的一些方法.第一个可以从同一站点获取标签的内容.

Here are some ways that I thought and found. First one can get content of a tag from same site.

    $.get("link.html", function(data) {
    var html = $('<div>').html(data);
    var content = html.find("div#some_div_id").html();
    //do something
    $("#Container").html(content);
});

使用.get()获取另一个页面中div的html

我尝试过,但对我不起作用.我认为是因为我的跨域请求.

I tried, but doesn't work for me. I think it's because of my cross domain request.

第二个想法是使用iframe.想要将页面加载到iframe中并提取主体标签.但是找不到关于如何检测iframe完成加载页面的事件的任何解决方案?或者如何从iframe加载的页面中获取内容?

Second idea is using iframe. Wanna load the page into iframe and extract body tag. But can't find any solution regarding how can I detect the event that iframe completed loading the page? Or how can I get the content from an iframe loaded page?

谁能告诉我如何以一种简单的方式从body标签中获取那些内容?像我的第一个示例一样,有什么解决方案吗?请帮助...

推荐答案

如果没有服务器端代理来请求其他站点的内容,这是不可能的.由于相同的原始政策,Javascript无法发出跨域请求.您只能使用iframe或AJAX(仅服务器端代理)来做到这一点.

This is not possible without a server-side proxy to request the other site's content; Javascript is not capable of making cross domain requests due to the same origin policy. You can't do this with an iframe or AJAX, server-side proxy only.

这篇关于从另一个域上托管的另一个页面的正文标签获取文本-javascript/jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆