一个 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");
}
}
1
civet 2016-01-08 00:15:31 +08:00 via iPhone
这个 public function 是哪里的?是在类里面还是什么,你有没有实例化了再调用?
还有 console.log()最好封装一下 看得累 |
2
leetom OP 看来是 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. |