简体   繁体   English

从远程服务器调用JavaScript函数

[英]Call JavaScript function from remote server

I tried to call JavaScript function exist on some server(server1) from another server(server2) and I got this error: 我试图从另一台服务器(server2)调用某些服务器(server1)上的JavaScript函数,我收到此错误:

Unsafe JavaScript attempt to access frame with URL https://server1/ from frame with URL https://server2/ . 不安全的JavaScript尝试与URL访问框架的https://服务器1 /从URL框架的https://服务器2 / Domains, protocols and ports must match. 域,协议和端口必须匹配。

I used JSP, Java, JavaScript and tomcat7, is there any way to solve this problem? 我用过JSP,Java,JavaScript和tomcat7,有什么方法可以解决这个问题吗? any help will be appreciated. 任何帮助将不胜感激。

Yes, must add a cross-origin rule to the header of your javascript file, which allows access from your other server. 是的,必须在您的javascript文件的标题中添加跨域规则,以允许从其他服务器进行访问。

Otherwise, your Browser doesn´t let you do that. 否则,您的浏览器不允许您这样做。

You can look at the answer of this Question: XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin 你可以看一下这个问题的答案: XmlHttpRequest错误:Access-Control-Allow-Origin不允许使用null

It should tell you how to do it. 它应该告诉你如何做到这一点。

Take a look at easyXDM - it provides an RPC feature allowing you to call methods across the Same Origin Policy . 看看easyXDM - 它提供了一个RPC功能,允许您跨同源 策略调用方法。

Take a look at one of the demo's here 看一下这里的一个演示

As described you are subject to the Same Origin Policy, this is designed to protect users. 如上所述,您遵循同源策略,旨在保护用户。 Google have a good write-up: http://code.google.com/p/browsersec/wiki/Part2 . 谷歌有一个很好的文章: http//code.google.com/p/browsersec/wiki/Part2

There are several typical approaches to working around this: 有几种解决此问题的典型方法:

  1. jquery has a getJson or jsonp type of function. jquery有一个getJson或jsonp类型的函数。 most other js libs have something similar. 大多数其他js libs都有类似的东西。 They use a dynamic Script tag, suitable for GET requests from other domains. 它们使用动态脚本标记,适用于来自其他域的GET请求。
  2. Create a servlet on domain1 that proxies to domain2 - allows unrestricted HTTP methods and use of XmlHTTPRequest. 在domain1上创建一个代理domain2的servlet - 允许不受限制的HTTP方法和使用XmlHTTPRequest。

I've not tried http://easyxdm.net/wp/ 我没试过http://easyxdm.net/wp/

There are improvements coming, like cross document messaging in HTML5 有一些改进,比如HTML5中的跨文档消息传递

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

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