2017-01-26 269 views
1

我想使用閃光燈發送帶有自定義頁眉POST請求發送POST請求與自定義標題,但我只能得到它來發送作爲報復,當我使用request.method = URLRequestMethod.POST;使用閃光燈

這裏的要求是我的代碼:

package { 
import flash.display.Sprite; 
import flash.events.*; 
import flash.net.*; 

public class URLRequestExample extends Sprite { 
    private var loader:URLLoader; 

    public function URLRequestExample() { 
     loader = new URLLoader(); 
     configureListeners(loader); 
     var url:String = "http://example.com/"; 
     var headers:Array = [ 
      new URLRequestHeader("X-CUSTOM-HEADER", "X-VALUE"), 
      new URLRequestHeader("Accept", "application/json") 
     ]; 
     var requestVars:URLVariables = new URLVariables(); 
     requestVars.test = "test"; 
     request.data = requestVars; // If this line and the one under it are commented the request goes as GET 
     request.requestHeaders = headers; // ^with no request headers 
     var request:URLRequest = new URLRequest(); 
     request.method = URLRequestMethod.POST; // Using URLRequestMethod.POST but the request is still sent as GET 
     request.url = url; 
     try { 
      loader.load(request); 
     } catch (error:Error) { 
      trace("Unable to load requested document."); 
     } 
    } 

    private function configureListeners(dispatcher:IEventDispatcher):void { 
     dispatcher.addEventListener(Event.COMPLETE, completeHandler); 
     dispatcher.addEventListener(Event.OPEN, openHandler); 
     dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler); 
     dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); 
     dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler); 
     dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
    } 

    private function completeHandler(event:Event):void { 
     var loader:URLLoader = URLLoader(event.target); 
     trace("completeHandler: " + loader.data); 
    } 

    private function openHandler(event:Event):void { 
     trace("openHandler: " + event); 
    } 

    private function progressHandler(event:ProgressEvent):void { 
     trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal); 
    } 

    private function securityErrorHandler(event:SecurityErrorEvent):void { 
     trace("securityErrorHandler: " + event); 
    } 

    private function httpStatusHandler(event:HTTPStatusEvent):void { 
     trace("httpStatusHandler: " + event); 
    } 

    private function ioErrorHandler(event:IOErrorEvent):void { 
     trace("ioErrorHandler: " + event); 
    } 
} 
} 

在我crossdomain.xml文件我有:

<?xml version="1.0"?> 
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd"> 
<cross-domain-policy> 
    <site-control permitted-cross-domain-policies="all"/> 
    <allow-access-from domain="*" secure="false"/> 
    <allow-http-request-headers-from domain="*" headers="*" secure="false"/> 
</cross-domain-policy> 
+0

1.您如何知道它以GET方式發送? 2.你把你的crossdomain.xml放在哪裏?通常,如果從Flash IDE運行測試(或者可以在全局設置中允許工作文件夾),則不需要它。 – Organis

+0

我使用'http:// wonderfl.net /'進行實時編碼(直接在瀏覽器上運行)。 我在根目錄中有我的crossdomain.xml文件。 這是我的代碼:http://wonderfl.net/c/ozHfE如果你想檢查你自己 – user00239123

回答

1

看來它處理請求,如GET如果沒有有效附加POST數據。只要我向URLVariables對象添加至少一個有效的鍵:值對,我的Chrome開發工具就會將請求方法報告爲POST:http://wonderfl.net/c/Ia2z

+0

謝謝,請求現在發送爲POST,但不幸的是自定義頭還沒有發送。你有什麼想法如何做到這一點? – user00239123

+0

我取消註釋了這些標題,並且我的Chrome開發工具報告中顯示了這些標題。 – Organis

+0

奇怪的是,我取消了他們的評論,他們沒有被髮送。它現在很好用。謝謝你的幫助! – user00239123