2017-09-15 121 views
2

我想發佈JSON到服務器,但代碼不工作。我發現了一些使用Actionscripts 3發佈JSON的例子,但我需要在代碼中定義內容類型。我在這裏發佈我的測試代碼。Actionscripts 3 POST JSON與內容頭

我測試了Firefox + RESTClient中的url + header + body並且他們工作。 [1] 我敢肯定,網址是真實的,而在另一個地方工作,但在這裏,我遇到錯誤 「錯誤打開URL」

URL [在這裏輸入的形象描述] [1]!]:」 https://api.thinger.io/v2/users/ *** 「

內容標題: 」應用程序/ JSON「

體:{」 在「:真}

import flash.display.Sprite; 
import flash.events.*; 
import flash.net.URLLoader; 
import flash.net.URLRequest; 
import flash.net.URLRequestHeader; 
import flash.net.URLRequestMethod; 
import flash.net.URLVariables; 


var loader: URLLoader; 

function ATN() { 
    loader = new URLLoader(); 
    configureListeners(loader); 

    var header: URLRequestHeader = new URLRequestHeader("Accept", "application/json"); 
    var request: URLRequest = new URLRequest("https://api.thinger.io/v2/users/***"); 
    request.data = new URLVariables("in:true"); 
    request.method = URLRequestMethod.POST; 
    request.requestHeaders.push(header); 
    try { 
     loader.load(request); 
    } catch (error: Error) { 
     trace("Unable to load requested document."); 
    } 

} 
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); 
} 

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

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

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

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

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

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

ATN(); 

ERROR:

ERR或者:錯誤#2101:傳遞給URLVariables.decode()的字符串必須是包含名稱/值對的URL編碼查詢字符串。

在錯誤$/throwError()

在flash.net::URLVariables/decode()

在flash.net::URLVariables()

在ATN_fla :: MainTimeline/ATN ()

在ATN_fla :: MainTimeline /幀1()

編輯:

function ATN() { 
    loader = new URLLoader(); 
    configureListeners(loader); 

    var header: URLRequestHeader = new URLRequestHeader("Accept", "application/json"); 
    var request: URLRequest = new URLRequest("https://api.thinger.io/v2/users/***"); 
    request.data = new URLVariables(); 
    var postData: Object = {"in": true}; 
    request.data = JSON.stringify(postData); 
    request.method = URLRequestMethod.POST; 
    request.requestHeaders.push(header); 
    try { 
     loader.load(request); 
    } catch (error: Error) { 
     trace("Unable to load requested document."); 
    } 

} 

OUTPUT: openHandler:[事件類型= 「開放」 氣泡=假或取消=假的EventPhase = 2] progressHandler加載:44總計:44

編輯2:

function ATN() { 
    loader = new URLLoader(); 
    configureListeners(loader); 

    var header: URLRequestHeader = new URLRequestHeader("Accept", "application/json"); 
    var request: URLRequest = new URLRequest("https://api.thinger.io/v2/users/***"); 
    var postData: Object = {"in": true}; 
    request.data = JSON.stringify(postData); 
    request.method = URLRequestMethod.POST; 
    request.requestHeaders.push(header); 
    try { 
     loader.load(request); 
    } catch (error: Error) { 
     trace("Unable to load requested document."); 
    } 

} 

OUTPUT 2 :

openHandler:[事件類型= 「開放」 氣泡=假或取消=假的EventPhase = 2] progressHandler加載:44總計:44

錯誤打開URL 'https://api.thinger.io/v2/users/ ***'

的httpStatusHandler:[HTTPStatusEvent類型= 「則httpStatus」 氣泡=假或取消=假的EventPhase = 2個狀態= 400重定向=假responseURL =空]

事件。ioErrorHandler:[IOErrorEvent type =「ioError」bubbles = false cancelable = false eventPhase = 2 text =「錯誤#2032:流錯誤。 URL:https://api.thinger.io/v2/users/ ***「]

+0

'錯誤#2032:流錯誤。URL:https://api.thinger.io/v2/users/***'確定URL找不到(URL不存在) (2)** https://api.thinger.io/**應該是有效的URL嗎?我看到一個_「404 Not Found」_,所以像'v2'和'users'這樣的文件夾怎麼存在? –

+0

網址正常。我測試了它在Firefox的RESTClient。在這裏我刪除了一部分URL。 –

+0

我更改了JASON數據,以前的錯誤消失了。現在出現新的錯誤。 –

回答

0

錯誤在於解析您提供給URLVariables構造函數的字符串。URLVariables僅用於構造名稱/值對,如果您想發送JSON,只需直接設置data屬性:

var postData:Object = { "in": true }; 
request.data = JSON.stringify(postData); 
+0

感謝您的回答,我更改了類似於編輯部分的代碼,但發生錯誤。我的代碼改變了嗎? –

+0

發生了什麼錯誤?您沒有提供有關您的問題的任何信息 – Michael

+0

我的新代碼是否正確?你可以在編輯部分看到新的錯誤。 –