简体   繁体   English

跨域脚本问题和JSONP

[英]Cross Domain Scripting Issues & JSONP

Our Client requires that we supply Widgits for their site. 我们的客户要求我们为其站点提供Widgits。 They want to link to us to get Html & the jQuery required to manipulate the Html and do asynchronous requests. 他们希望链接到我们以获得Html和操纵Html并执行异步请求所需的jQuery。 I understand that there are cross-domain security limitations that would prevent this from being a possibility, but that some of those limitations are aleviated by using JSONP as the data transfer format. 我知道存在跨域安全限制,可以防止这种情况的发生,但是通过使用JSONP作为数据传输格式可以缓解其中的一些限制。

I'm finding it difficult in finding an explanation of what's possible in the context of what I'm trying to achieve. 我很难找到关于我要达到的目标的可能解释。 Could somebody please fill me in? 有人可以帮我吗?

In short, all AJAX requests (and cross-window scripting) are subject to the Same Origin Policy . 简而言之,所有AJAX请求(和跨窗口脚本)都应遵循“ 相同来源策略” JSONP ( JSON with Padding ) isn't subject to the Same Origin Policy because it involves adding a script from an external domain to the DOM, the script itself contains a call to a known function that already exists on the client, with the JSON as the function call's argument. JSONP( 带填充的JSON )不受相同来源政策的约束,因为它涉及将来自外部域的脚本添加到DOM,脚本本身包含对客户端上已经存在的已知函数的调用,JSON为函数调用的参数。

JSONP can't return HTML or XML directly, but it could pass an object that contains a string of HTML or XML data, which in turn could be added to the DOM or parsed by the client. JSONP无法直接返回HTML或XML,但它可以传递包含HTML或XML数据字符串的对象,而该对象又可以添加到DOM或由客户端解析。

For instance, a JSONP might return: 例如,JSONP可能返回:

jsonp_callback({"Errors":"none","Data":"<div id='externalWidget'>Hello!</div>"});

When this script is added to the page, the function jsonp_callback will be executed with the JSON object as its argument. 当将此脚本添加到页面时,将以JSON对象作为参数执行函数jsonp_callback That function would then add the HTML code to the page. 然后,该函数会将HTML代码添加到页面中。

There are other ways of achieving what you want. 还有其他方式可以实现您想要的。 For instance, if the client doesn't need to manipulate the data in any way, you could provide a widget via a HTML document that would be iframed by your client's page: 例如,如果客户端不需要以任何方式处理数据,则可以通过HTML文档提供一个小部件,该小部件将由您的客户端页面填充:

<iframe id="widget" src="http://mysite.com/widget/v1/" />

If they did need to manipulate the data, they would blocked by the Same Origin Policy as outlined above. 如果他们确实需要操纵数据,则将受到如上所述的“相同来源策略”的阻止。

As far as I know, JSONP utilises SCRIPT tags to load content that is external to the domain that your calling page is being loaded from. 据我所知,JSONP利用SCRIPT标记来加载从其加载呼叫页面的域外部的内容。 Using a SCRIPT tag allows you to reference external URLs. 使用SCRIPT标记可以引用外部URL。 Once this external 'script' has been loaded, data will be returned to a specific callback function which was passed through the location of the external script. 加载此外部“脚本”后,数据将返回到特定的回调函数,该函数通过外部脚本的位置传递。

jQuery: http://www.insideria.com/2009/03/what-in-the-heck-is-jsonp-and.html jQuery: http//www.insideria.com/2009/03/what-in-the-heck-is-jsonp-and.html

MooTools: http://www.clientcide.com/wiki/cnet-libraries/06-request/00-jsonp MooTools: http : //www.clientcide.com/wiki/cnet-libraries/06-request/00-jsonp

It sounds like you could use easyXDM ( http://easyxdm.net/ ) for your purpose. 听起来您可以出于自己的目的使用easyXDM( http://easyxdm.net/ )。 Its a library that gives you cross-domain messaging and RPC. 它是一个库,可为您提供跨域消息传递和RPC。

You can easily use it to set up an iframe based widget (like the one the build int Widget class does http://consumer.easyxdm.net/current/example/widgets.html ) or use it only to get raw data. 您可以轻松地使用它来设置基于iframe的窗口小部件(例如build int窗口小部件类执行http://consumer.easyxdm.net/current/example/widgets.html的窗口小部件)或仅将其用于获取原始数据。

You can also take a look at the xhr sample that gives access to cross-domain ajax at http://consumer.easyxdm.net/current/example/xhr.html , or the generic RPC sample at http://consumer.easyxdm.net/current/example/methods.html 您还可以在http://consumer.easyxdm.net/current/example/xhr.html上查看可以访问跨域ajax的xhr示例,或者在http://consumer.easyxdm上使用通用的RPC示例。 达网络/电流/示例/ methods.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM