V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
leetom
V2EX  ›  问与答

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

  •  
  •   leetom · 2016-01-07 22:15:28 +08:00 · 1842 次点击
    这是一个创建于 3003 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个 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");
            }
        }
    
    3 条回复    2016-01-08 17:51:04 +08:00
    civet
        1
    civet  
       2016-01-08 00:15:31 +08:00 via iPhone
    这个 public function 是哪里的?是在类里面还是什么,你有没有实例化了再调用?
    还有 console.log()最好封装一下 看得累
    leetom
        2
    leetom  
    OP
       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
        3
    civet  
       2016-01-08 17:51:04 +08:00
    @leetom 是的,限制了,需要用户交互去触发
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3661 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 05:02 · PVG 13:02 · LAX 22:02 · JFK 01:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.