简体   繁体   English

在flex(Web控件)中使用angularjs并尝试加载templates / json(本地文件)时出现跨域错误

[英]Getting cross-domain errors when using angularjs within flex (web control), and attempting to load templates/json (local files)

Let me give you a bit of history on what I'm trying to do. 让我告诉你一些关于我正在做什么的历史。

I'm using a Flex application, as a container for a web app. 我正在使用Flex应用程序,作为Web应用程序的容器。 So within this Flex app, it has a web controller, which loads a full fledged Angularjs application. 因此,在这个Flex应用程序中,它有一个Web控制器,可以加载一个完整的Angularjs应用程序。 All the files are stored locally within the Flex app. 所有文件都存储在Flex应用程序中。

When I attempt to run it, it cannot load any of the templates; 当我尝试运行它时,它无法加载任何模板; they are loaded using the $routeProvider, and data is pulled using the $http.get. 它们使用$ routeProvider加载,并使用$ http.get拉取数据。

The core problem is that it's not allowing me to pull local files using JavaScript, even though it's being run off the file-system (not using a web server). 核心问题是它不允许我使用JavaScript来提取本地文件,即使它是从文件系统(不使用Web服务器)运行的。

I also tested it on Chrome, and have the same exact issue - giving the following error. 我也在Chrome上测试了它,并且具有相同的问题 - 给出以下错误。

Origin null is not allowed by Access-Control-Allow-Origin.

With Chrome, it can be fixed using --allow-file-access-from-files , but can't figure out how to get it working via the Flex/Flash web controller. 使用Chrome,可以使用--allow-file-access-from-files修复它,但无法弄清楚如何通过Flex / Flash Web控制器使其工作。

Any help would be greatly appreciated. 任何帮助将不胜感激。

If you haven't already done so. 如果你还没有这样做的话。 Try setting up a crossdomain policy for your application. 尝试为您的应用程序设置跨域策略。

The problem is definitely due to CORS , the browser enforces this security constraint. 问题肯定是由于CORS ,浏览器强制执行此安全约束。 In your case the browser is the flex web control. 在您的情况下,浏览器是flex web控件。

You may not be able to get around this problem without using a web server. 如果不使用Web服务器,您可能无法解决此问题。 When the request is made for a file, the Cross Origin Resource Sharing policy will not allow the resource to be loaded without prior authorization from the host serving the file, This authorization comes in the form of an http header such as the following: 当对文件发出请求时,跨源资源共享策略将不允许在未事先授权的情况下从提供文件的主机加载资源。此授权以http头的形式出现,如下所示:

Access-Control-Allow-Origin: *

Unfortunately, you probably can't. 不幸的是,你可能不会。 I don't know what engine Flex wraps in it's web controllers, but the reason you are bumping in to this is security. 我不知道Flex包含在它的Web控制器中的引擎是什么,但是你碰到这个的原因是安全性。

It's in place for good reason, it's to prevent JS from having direct access to your file system. 它有充分的理由,这是为了防止JS直接访问您的文件系统。

I would suggest one of two workarounds. 我建议使用两种解决方法之一。

此外,您可以尝试使用$ http.jsonp而不是$ http.get,尽管您可能需要更改angularJS代码....

I believe there are at least 2 implementations of rendering a webpage within Flex. 我相信在Flex中至少有两种渲染网页的实现。 I quickly checked the docs for StageWebView and found that the file:// uri scheme allows for Local XMLHttpRequests (basically what $http.get is doing). 我快速检查了StageWebView的文档,发现file:// uri方案允许本地XMLHttpRequests(基本上是$ http.get正在做什么)。

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

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