【问题标题】:Post data to url without navigating to it?将数据发布到 url 而不导航到它?
【发布时间】:2012-06-10 20:58:46
【问题描述】:

我对 Flash 和 AS3 完全陌生。我在 Google 上搜索过,但找不到关于这个主题的任何内容。

我有一些将图像发布到 php 文件的代码:

var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
    var jpgURLRequest:URLRequest = new URLRequest("http://127.0.0.1/gdipORG/takeImage.php");
    jpgURLRequest.requestHeaders.push(header);
    jpgURLRequest.method = URLRequestMethod.POST;
    jpgURLRequest.data = byteArray;
navigateToURL(jpgURLRequest, "blank");

问题是,要进行实际发布,我必须导航到实际 url(参见最后一行代码)...

我希望能够将数据发布到 url 而无需导航到它。有什么想法吗?

【问题讨论】:

  • 查看 AJAX。它在幕后发布,而不必更改页面。
  • @JonathanM 虽然这是在 Flash 中...我不能在 Flash 中使用 AJAX,对吧?

标签: php actionscript-3 flash post httpwebrequest


【解决方案1】:

在现有代码之后使用 URLLoader:

//Existing code
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var jpgURLRequest:URLRequest = new URLRequest("http://127.0.0.1/gdipORG/takeImage.php");
jpgURLRequest.requestHeaders.push(header);
jpgURLRequest.method = URLRequestMethod.POST;
jpgURLRequest.data = byteArray;

//KILL THIS LINE navigateToURL(jpgURLRequest, "blank");

//Add these (UNTESTED CODE):

var sendJPGLoader:URLLoader = new URLLoader();
sendJPGLoader.dataFormat = URLLoaderDataFormat.BINARY;
sendJPGLoader.addEventListener(Event.COMPLETE, sendJPGToServerComplete);
sendJPGLoader.addEventListener(IOErrorEvent.IO_ERROR, sendJPGToServerIOError);

//Try to send image
sendJPGLoader.load(jpgURLRequest); 

function sendJPGToServerComplete(evt:Event):void {
  //Request was sent to server succesfully
  //Optionally check server response
  // var serverResponse:String = String(evt.target.data);
}

function sendJPGToServerIOError(evt:Event):void {
  //Failed
}

不要忘记在代码顶部导入:

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;

我想我都明白了:)

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 2016-01-19
    • 2019-01-25
    相关资源
    最近更新 更多