使用JavaScript跨域内容加载 [英] Cross domain content loading with javascript

查看:66
本文介绍了使用JavaScript跨域内容加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,这是问题所在

远程站点需要从其他域中提取复杂的html页面内容.由于无法确定页面高度,因此iframe解决方案不够好,并且IE浏览器中不允许跨域Ajax.有JSONP,但是需要提供的代码量太复杂了,我们还需要提供一些功能.

The remote site needs to pull complex html page content from other domain. Iframe solution is not good enough because of the problem with determining the page height, and cross domain ajax is not allowed on IE browsers. There is JSONP but amount of code that needs to be served is too complex and we also need to serve some funcionalities.

解决方案:

在服务器端,我们正在动态生成JavaScript,该JavaScript在字符串变量中包含所有复杂的html内容.在远程方面,我们只包括对此脚本的调用,然后在页面上放置一个.然后,Div中会填充来自服务变量的内容.它可以在所有浏览器上跨域工作,并且内容可以完美显示:)).

On server side we are dinamically generating javascript that contains all the complex html content in a string variable. On remote side we just include call to this script and we put a on the page. Div is then filled with content from served variable. It works cross domain on ALL browsers and the content displays perfectly :)).

问题:

这种方法有什么问题?为什么在线上任何地方都没有提到这样的解决方案?对于提供任何类型的小部件和类似内容来说,这似乎都是完美的选择,但现在我深感其中有些谬误:)?

What's wrong with this approach? Why there is no mention of such solution anywhere online? It seems perfect for serving any kind of widgets and alike content, and now I'm affraid there is some big fallacy in it:)?

请对它进行拆封:)

推荐答案

这种方法没有问题.出于安全考虑,在客户端禁止跨域,但是在服务器端,您可以按照自己的意愿进行操作.

There is no problem with this approach. Cross domain is forbidden on client side for security matters, but on server side you do as you wish.

此处的区别在于,您必须重新加载页面才能查询其他域,然后将其推送到您的页面.因此,基本上,您可以执行以下操作:

The difference here is that you have to reload a page in order for it to query the other domain and then push it to your page. So basically you could do the following :

1-您的JavaScript对其自己的域进行了ajax请求.2-在服务器端,您进行跨域请求3-您打印一些供请求者使用的js/html

1- Your javascript does an ajax request to it's own domain. 2- On the server side, you make a cross domain request 3- You print some js/html to be used by the requester

在这种情况下,您的请求比直接在客户端执行的请求多.

In that scenario, you have one more request than doing it directly on the client side.

这篇关于使用JavaScript跨域内容加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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