简体   繁体   English

AJAX加载小部件的同源策略

[英]same-origin policy with AJAX load widget

I am trying to include a widget in my webpage. 我正在尝试在网页中包含小部件。 The code for the widget is loaded dynamically with ajax (because it changes often and I need to update it from the server) and it looks like this ... 小部件的代码使用ajax动态加载(因为它经常更改,因此我需要从服务器进行更新),看起来像这样……

<a class="e-widget" href="https://gleam.io/0oIpw/contest-widget" rel="nofollow">This is a Widget!</a>

<script type="text/javascript" src="https://js.gleam.io/e.js" async="true"></script>

on load, I get the following errors in the console... 在加载时,控制台中出现以下错误...

OPTIONS https://js.gleam.io/e.js 404 (Not Found)
XMLHttpRequest cannot load https://js.gleam.io/e.js. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:8443' is therefore not allowed access. The response had HTTP status code 404.

If I remove the ajax call that loads the data for the widget, and instead insert the widget directly, I do not get the same errors and the widget works fine. 如果我删除了加载小部件数据的ajax调用,而是直接插入了小部件,则不会出现相同的错误,并且小部件可以正常工作。

I have read into this and figure that it is due to the Same-Origin-Policy (SOP), so I am now wondering the best way to circumvent the policy. 我已阅读并认为这是由于“原产地政策”(SOP)所致,所以我现在想知道规避该政策的最佳方法。

I have read the post Ways to circumvent the same-origin policy but unfortunately did not find it helpful in this case. 我已经阅读了规避同源政策的方法,但不幸的是,在这种情况下,它没有帮助。

Since CORS is done on the server side (I think ? ) and JSONP is insecure, is the best option to create a proxy? 由于CORS是在服务器端完成的(我认为?),而JSONP是不安全的,因此创建代理的最佳选择是吗?

Thanks so much for the help. 非常感谢帮忙。 I have spent quite a few hours researching this and I am still confused. 我花了好几个小时研究这个问题,但仍然感到困惑。

Edited to add code for more info : 编辑添加代码以获取更多信息:

The information for the page is loaded via ajax when a command link is clicked as follows : 单击命令链接时,通过ajax加载页面信息,如下所示:

<h:commandLink action="#{redeemPerk.getDisplay(display.displayId)}" >
                    <h:graphicImage value="#{display.imgUrl}" styleClass="display-icon"/>
<f:ajax event="click" execute="@form" render="redeem-display-data-reveal" listener="#{redeemPerk.getDisplay(display.displayId)}" onevent="handleAjax"/>
</h:commandLink>

this renders the area that displays the widget, which looks like ... 这将呈现显示小部件的区域,看起来像...

    <div class="reveal-modal-background hidden">
        <h:form id="redeem-display-data-reveal">
           <h:panelGroup rendered="#{display.type == 'WIDGET'}">
             <a class="e-widget" href="https://gleam.io/0oIpw/contest-widget" rel="nofollow">This is a Widget!</a>

           <script type="text/javascript" src="https://js.gleam.io/e.js" async="true"></script>
        </h:form>
       </div></h:panelGroup>

The second chunk of code is in a separate file from the first. 第二个代码块与第一个代码块位于不同的文件中。 To reiterate, if I remove the ajax call and load the data directly the widget works fine. 重申一下,如果我删除了ajax调用并直接加载数据,则小部件可以正常工作。

I am seeing two things in your output log that could be causing the issue. 我在您的输出日志中看到两件事,这可能是导致问题的原因。

First, it states that you received a 404 message from the request. 首先,它指出您从请求中收到了404消息。 Which means the JavaScript has probably not been uploaded properly. 这意味着JavaScript可能未正确上传。

Second, it says that the origin of the request came from localhost:8443. 其次,它说请求的来源来自本地主机:8443。 That leads me to believe that you are running the code locally instead of from the Internet. 这使我相信您是在本地而不是从Internet运行代码。

In cases where you are trying to load a plugin from the internet, but your code is being tested locally you are still going to get an SOP error. 如果您尝试从Internet加载插件,但代码正在本地测试,则仍然会出现SOP错误。 To fix this problem you would need to upload all of the code that you do have to your web server. 要解决此问题,您需要将所需的所有代码上载到Web服务器。 Once you have done that attempt to load the webpage from the Internet and not your local copy. 完成此操作后,尝试从Internet而不是本地副本加载网页。 That should fix that SOP error. 那应该解决那个SOP错误。

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

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