JavaScript中的跨域数据访问 [英] Cross-domain data access in JavaScript

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

问题描述

我们有一个ASP.Net应用程序托管在我们的网络上,并暴露给特定的客户端。此客户端希望能够将数据从自己的服务器导入到我们的应用程序中。数据使用HTTP请求检索,并采用CSV格式。问题是他们不想将他们的服务器暴露给我们的网络,并且正在请求在客户端进行导入(所有客户端都来自与其服务器相同的网络)。

We have an ASP.Net application hosted on our network and exposed to a specific client. 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. 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).

所以,需要做的是:



  1. 页面上的客户端脚本向其服务器发出请求以获取CSV格式的数据

  2. 数据发送回我们的应用程序

这两个服务器都在同一个域上不是一个挑战:一个简单的隐藏的iframe或类似的东西会做的,但这里我得到的是一个跨域访问被拒绝的错误。他们还拒绝更改数据格式以返回JSON或XML格式的数据。

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. They also refuse to change the data format to return JSON or XML formatted data.

到目前为止,我尝试和学习的是:

What I tried and learned so far is:


  1. 隐藏的iframe - 访问被拒绝

  2. XMLHttpRequest - 行为取决于浏览器的安全设置:可能工作,

  3. 动态脚本标记 - 如果他们可以返回JSON格式的数据,则会生效

  4. IE客户端数据绑定 - 相同的访问被拒绝错误

  1. Hidden iframe -- "access denied"
  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
  3. Dynamic script tags -- would have worked if they could have returned data in JSON format
  4. IE client data binding -- the same "access denied" error

在放弃之前还有其他可以尝试,将不可能没有暴露他们的服务器到我们的应用程序,更改其数据格式或更改其浏览器安全设置?

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).

推荐答案

这可能为你的客户端太晚了,可以控制这两个网域,您可以尝试 EasyXDM 。它是一个包装跨浏览器怪癖的库,并提供一个易于使用的API,用于在不同域之间使用该浏览器的最佳可用机制在客户端脚本中进行通信(例如 postMessage (如果可用,其他机制,如果没有)。

It might be too late for your client, but since you have have control over both domains, you can try 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 (e.g. postMessage if available, other mechanisms if not).

注意:您需要控制两个域才能使其工作(其中控制意味着您可以在其中放置静态文件)。但您不需要任何服务器端代码更改。

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.

这篇关于JavaScript中的跨域数据访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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