AS3 + HTTP GET不起作用 [英] AS3 + HTTP GET doesn't work

查看:165
本文介绍了AS3 + HTTP GET不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Flash中的项目,我使用了一些数据的网络服务器。我读到的信息(JSON)有:

 变种网址:字符串=URL请求;
VAR要求:的URLRequest =新的URLRequest(URL);
VAR装载机:的URLLoader =新的URLLoader();
loader.load(要求);
 

和我使用的文本字段的信息。这工作正常,并正确显示我的数据。但是,当我发表我的工作或打开.swf文件不显示数据的文件。

里面的Adobe Flash正常工作。 外面是行不通的。

我有一个树莓派在运行nodeJS的服务。门是在路由器开放

我nodeJS

  VAR EX preSS =需要('EX preSS');
VAR bodyParser =需要('身体解析器');
VAR应用= EX preSS();
app.use(bodyParser.json());
app.use(bodyParser.urlen codeD({扩展:真}));
常量口= process.env.PORT || 3001;
常量SERVER_ROOT =HTTP://本地主机:+端口;
变种消息= {};
消息[A1] = blablabla;
 - 
消息[N] = blablabla;
功能buildMessage(NEWID,文本,用户){
常量现在=新的日期();
返回 {

    };
};
app.route(/消息)
获得(功能(REQ,RES){
    res.header(访问控制 - 允许 - 起源,*);
    res.header(访问控制 - 允许 - 头,X-要求,随着);

    res.json(消息);
});
app.param('MESSAGEID',功能(REQ,资源,接下来,邮件ID){
req.messageID = MESSAGEID;
返回下一个();
})
app.listen(端口,函数(){
的console.log(听的+端口);
});
 

解决方案

这是被解雇当SWF尝试加载另一个域之外的其它内容,并没有拥有授权做一个经典的安全错误。因此,为了避免这种类型的安全错误的,你必须创建一个的crossdomain.xml 文件在服务器从要加载数据的根源。为了更明白的事情,采取这种模式一看(取,和编辑,从<一个href="http://www.adobe.com/devnet-docs/acrobatetk/tools/AppSec/CrossDomain_PolicyFile_Specification.pdf"相对=nofollow>的Adobe跨域策略文件):

因此,在这种情况下,允许在 a.com 的SWF文件从 b.com 加载数据,我们有在 b.com 的根添加的crossdomain.xml 文件,所以我们可以利用访问它 b.com/crossdomain.com 。对于这个文件,有关它的更多细节的内容,你可以看到上面的跨域规范的链接。它可以是这样的:

 &LT; XML版本=1.0&GT?;
&lt;交域政策&GT;
    &LT;! - 如果单独使用,仅允许所有a.com请求,但不是它的子域 - &GT;
    &LT;允许存取来自域=a.com/&GT;
    &LT;! - 如果单独使用,允许所有a.com子域和a.com请求 - &GT;
    &LT;* a.com允许访问,从域= /&GT;
    &LT;! - 如果单独使用,仅允许bacom子域的请求 - &GT;
    &LT;允许存取来自域=b.a.com/&GT;
&LT; /跨域策略&GT;
 

我希望这一切可以帮助你解决你的问题。

I have a project in Flash and I use a webserver with some data. I read that information (json) with:

var url:String = "URL REQUEST";
var request:URLRequest = new URLRequest(url);
var loader:URLLoader = new URLLoader();
loader.load(request);

and I use that information in a TextField. This works fine and show my data properly. But, when I publish my work or open the file .swf doesn't show the data.

Inside the Adobe Flash works fine. Outside doesn't work.

I have a raspberry pi with a service in nodeJS running. The door is open in the router.

My nodeJS

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
const port = process.env.PORT || 3001;
const SERVER_ROOT = "http://localhost:" + port;
var messages =  {};
messages["a1"] = blablabla;
--
messages["n"] = blablabla;
function buildMessage(newID, text, user){
const now = new Date();
return {

    };
};
app.route("/message") 
.get(function(req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");

    res.json(messages);
});
app.param('messageID', function(req, res, next, messageID){
req.messageID = messageID;
return next();
})
app.listen(port, function() {
console.log("Listening on " + port);
});

解决方案

This is a classic security error which is fired when an swf try to load content in another domain other than its own and didn't has authorization to do that. So to avoid this type of security error, you have to create a crossdomain.xml file at the root of the server from where you want to load data. To more understand things, take a look on this schema ( taken, and edited, from Adobe Cross Domain Policy File) :

So in this case, to allow an swf file in a.com to load data from b.com, we have to add a crossdomain.xml file in the root of b.com, so we can access to it using b.com/crossdomain.com. For the content of this file and more details about it, you can see the link above of the crossdomain specification. It can be like this :

<?xml version="1.0"?>
<cross-domain-policy>
    <!-- if used alone, allow only all a.com requests but not its sub-domains -->
    <allow-access-from domain="a.com"/>
    <!-- if used alone, allow all a.com sub-domains and a.com requests -->
    <allow-access-from domain="*.a.com"/>
    <!-- if used alone, allow only b.a.com sub-domain requests -->
    <allow-access-from domain="b.a.com"/>
</cross-domain-policy>

I hope all this can help you to resolve your problem.

这篇关于AS3 + HTTP GET不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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