碰到个很奇怪的问题,用 JS 调用 AS 的函数,和直接用 AS 调用,同一个函数,行为不一样。

2016-01-07 22:15:28 +08:00
 leetom

一个 AS 函数提交 POST 请求到服务器,但是当通过ExternalInterface.addCallback暴露给 JS ,用 JS 调用时,该请求就直接被忽略了,也没有什么错误或者异常出现。

这个是什么原因呢?好奇怪

public function saveFileToServer(data:ByteArray, filename:String, whenDone:Function):void {
        var values:URLVariables = new URLVariables();
        values.key = "Message";

        var header:URLRequestHeader = new URLRequestHeader("pragma", "no-cache");
        var request:URLRequest = new URLRequest("/save");
        var data = getData();// function to get data to post

        request.contentType = "multipart/form-data; boundary=" + UploadPostHelper.getBoundary();
        request.requestHeaders.push(header);
        request.method = URLRequestMethod.POST;
        request.data = UploadPostHelper.getPostDataMultiFiles(
            {
                data:data
            } );

        var loader:URLLoader = new URLLoader();
        loader.dataFormat = URLLoaderDataFormat.BINARY;
        loader.addEventListener(Event.COMPLETE, whenDone);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,function(err:ErrorEvent):void
        {
            Scratch.app.logMessage("Failed server request for ");
            ExternalInterface.call("console.log", "SECURITY_ERROR");
        });
        loader.addEventListener(IOErrorEvent.IO_ERROR,function(err:ErrorEvent):void
        {
            Scratch.app.logMessage("Failed server request for ");
            ExternalInterface.call("console.log", "IO_ERROR");
        });
        loader.addEventListener(ProgressEvent.PROGRESS,function(event:ProgressEvent):void
        {
            ExternalInterface.call("console.log", "PROGRESS");
        });
        try {
            ExternalInterface.call("console.log", "Before load");  //正常输出
            loader.load(request);    // 用 JS 调用时,没有效果,没有请求出现。
            ExternalInterface.call("console.log", "After load");   //正常输出
        }
        catch (error:ArgumentError) {
            trace("An ArgumentError has occurred."); // no error caught.
        }
        catch (error:SecurityError) {
            trace("A SecurityError has occurred.");
        }catch (error:*){
            trace("other errors");
        }
    }
1871 次点击
所在节点    问与答
3 条回复
civet
2016-01-08 00:15:31 +08:00
这个 public function 是哪里的?是在类里面还是什么,你有没有实例化了再调用?
还有 console.log()最好封装一下 看得累
leetom
2016-01-08 10:13:22 +08:00
看来是 Flash 的安全策略问题

JS 调用不是响应用户事件( JS 是响应用户事件,应该是 Flash 捕捉不到 JS 的用户事件),所以不能上传文件。

The POST operation must be performed in response to a user-initiated action, such as a mouse click or key press.
If the POST operation is cross-domain (the POST target is not on the same server as the SWF file that is sending the POST request), the target server must provide a URL policy file that permits cross-domain access.
civet
2016-01-08 17:51:04 +08:00
@leetom 是的,限制了,需要用户交互去触发

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/249148

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX