简体   繁体   English

JavaScript中的跨域数据访问

[英]Cross-domain data access in JavaScript

We have an ASP.Net application hosted on our network and exposed to a specific client. 我们在我们的网络上托管了一个ASP.Net应用程序,并向特定客户端公开。 This client wants to be able to import data from their own server into our application. 该客户希望能够将自己服务器中的数据导入我们的应用程序。 The data is retrieved with an HTTP request and is CSV formatted. 使用HTTP请求检索数据并进行CSV格式化。 The problem is that they do not want to expose their server to our network and are requesting the import to be done on the client side (all clients are from the same network as their server). 问题是他们不想将他们的服务器暴露给我们的网络,并且要求在客户端进行导入(所有客户端都来自与他们的服务器相同的网络)。

So, what needs to be done is: 所以,需要做的是:

  1. They request an import page from our server 他们从我们的服务器请求导入页面
  2. The client script on the page issues a request to their server to get CSV formatted data 页面上的客户端脚本向其服务器发出请求以获取CSV格式的数据
  3. The data is sent back to our application 数据将发送回我们的应用程序

This is not a challenge when both servers are on the same domain: a simple hidden iframe or something similar will do the trick, but here what I'm getting is a cross-domain "access denied" error. 当两个服务器位于同一个域时,这不是一个挑战:一个简单的隐藏iframe或类似的东西可以解决这个问题,但在这里我得到的是跨域“访问被拒绝”错误。 They also refuse to change the data format to return JSON or XML formatted data. 他们还拒绝更改数据格式以返回JSON或XML格式的数据。

What I tried and learned so far is: 到目前为止我尝试和学到的是:

  1. Hidden iframe -- "access denied" 隐藏的iframe - “访问被拒绝”
  2. XMLHttpRequest -- behaviour depends on the browser security settings: may work, may work while nagging a user with security warnings, or may not work at all XMLHttpRequest - 行为取决于浏览器安全设置:可能有效,可能在用安全警告唠叨用户时工作,或者可能根本无法工作
  3. Dynamic script tags -- would have worked if they could have returned data in JSON format 动态脚本标记 - 如果它们可以以JSON格式返回数据,则会起作用
  4. IE client data binding -- the same "access denied" error IE客户端数据绑定 - 相同的“访问被拒绝”错误

Is there anything else I can try before giving up and saying that it will not be possible without exposing their server to our application, changing their data format or changing their browser security settings? 在放弃之前还有什么我可以尝试的,并且说如果不将他们的服务器暴露给我们的应用程序,改变他们的数据格式或改变他们的浏览器安全设置是不可能的吗? (DNS trick is not an option, by the way). (顺便说一句,DNS技巧不是一个选项)。

JSONP might be the answer for you if they can server data in a JSON format. 如果JSONP可以以JSON格式提供服务数据,那么JSONP可能就是您的最佳选择。 Other than that you will always run into Same Origin Policy issues with cross-domain calls. 除此之外,您将始终遇到跨域调用的同源策略问题。 Have you looked into doing Server-side calls to do HTTP requests to their server? 您是否考虑过执行服务器端调用来向其服务器发出HTTP请求?

It might be too late for your client, but since you have have control over both domains, you can try EasyXDM . 对于您的客户来说可能为时已晚,但由于您可以控制这两个域,因此您可以尝试使用EasyXDM It's a library which wraps cross-browser quirks and provides an easy-to-use API for communicating in client script between different domains using the best available mechanism for that browser (eg postMessage if available, other mechanisms if not). 它是一个包含跨浏览器怪癖的库,它提供了一个易于使用的API,用于使用该浏览器的最佳可用机制在不同域之间的客户端脚本中进行通信(例如,如果可用,则为postMessage,否则为其他机制)。

Caveat: you need to have control over both domains in order to make it work (where "control" means you can place static files on both of them). 警告:您需要控制两个域才能使其工作(“控制”意味着您可以在两个域上放置静态文件)。 But you don't need any server-side code changes. 但是您不需要任何服务器端代码更改。

Your client is JavaScript served by your application, right? 您的客户端是您的应用程序提供的JavaScript,对吧?

Your client can then only send requests to your applciation (cross-site scripting prevention), that the error you are seeing? 然后,您的客户端只能向您的applciation发送请求(跨站点脚本防护),您看到的错误是什么?

Assuming yes, then a solution is to have your application offer a "proxy" service. 假设是,那么解决方案是让您的应用程序提供“代理”服务。 You browser code can ask your server for some data. 您的浏览器代码可以向您的服务器询问一些数据。 Your server is free to issue an Http request to any server it likes (no browser to object). 您的服务器可以自由地向它喜欢的任何服务器发出Http请求(没有浏览器对象)。 So you implement a little service to go get that cvs data and present it to your application. 因此,您可以实现一些小服务来获取cvs数据并将其呈现给您的应用程序。

You might even choose to map that CSV data to JSON if it's your client that's consuming it. 如果您的客户正在使用它,您甚至可以选择将该CSV数据映射到JSON。

Can't you host just the JS file on their server? 你不能只在他们的服务器上托管JS文件吗? This should allow script in that file to make ajax calls back to their server. 这应该允许该文件中的脚本将ajax调用回服务器。

You can try though the flash. 你可以试试闪光灯。 If you will put this yourdomain.com/crossdomain.xml in you root you will be able to make cross domain requests from mysite.com. 如果你将这个yourdomain.com/crossdomain.xml放在你的root中,你就可以从mysite.com发出跨域请求。

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="www.mysite.com" to-ports="25" />
</cross-domain-policy>

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

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