跨域发布页面源的最佳方法?
我试图弄清楚是否有一种方法可以在每次加载页面时使用 javascript 或其他方法跨域传递页面源?我知道在 javascript 中我可以使用下面的代码将页面源输出到变量中,但是我如何将其传递到不同的域?
var head_src = document.head.innerHTML;
var body_src = document.body.innerHTML;
有人能指出我正确的方向吗?似乎我尝试或研究过的每种方法都无法正常工作或存在很多问题,而且我已经没有想法了。提前致谢。
编辑下面的更多信息
想象一下,我在不同的服务器上有两个网站。网站 A 是源数据需要发送到的地方,网站 B 是用户浏览的网站,我想收集他们查看的每个页面的源并将其发布到网站 A。我可以访问这两个服务器,我希望我可以在网站 B 上添加 JS 代码来传递数据。
I'm try to figure out if there is a way to pass the page source across domains using javascript or another method each time a page is loaded? I know in javascript I can output the page source in to variables using the code below, but how would I go about passing it over to a different domain?
var head_src = document.head.innerHTML;
var body_src = document.body.innerHTML;
Can anyone point me in to the right direction? It seems every method I've tried or researched doesn't work correctly or has a lot of issues and I'm running out of ideas. Thanks in advance.
EDIT MORE INFO BELOW
Imagine that I have two websites on different servers. Website A is where the source data needs to be sent to and Website B is a website that users browse and I want to collect the source of each page they view and post it to Website A. I have access to both servers and I was hoping that I could add JS code on Website B that will pass the data over.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用您选择的服务器端语言发出请求并将数据存储在 JavaScript 中。这种代理方法是从不同域抓取数据的常用方法。
Use your server-side language of choice to make the request and store the data in javascript. This proxying method is a common way of grabbing data from different domains.
只需使用您描述的方法捕获源代码,然后使用 JS 将其发布到表单中(即 POST 变量)。
Just capture the source using the method you described then use JS to post it in a form (ie, POST variable).
我不相信您能够在客户端执行此操作,因为 innerHTML 会根据浏览器产生不同的输出。各种版本的 Internet Explorer 尤其不尊重原始源代码 - 您会发现到处都插入了未加引号的属性、混合大小写标签和额外标记。
我能想到的唯一解决方案是将服务器 B 的 HTML 输出缓冲到输出页面中的转义表单字段中,并将其发布到服务器 A。如何进行缓冲将取决于服务器 B 运行的平台。
I don't believe you'll be able to do this client-side because innerHTML produces different output depending on the browser. Various versions of Internet Explorer especially won't respect the original source code - you'll find unquoted attributes, mixed case tags and extra markup inserted all over the place.
The only solution I can think of is to buffer the HTML output from server B into an escaped form field in the output page and have that posted across to server A. How you do the buffering will depend on what platform server B is running.