我正在尝试将JSON发布到服务器但代码无效。我找到了一些使用Actionscripts 3发布JSON的示例,但我需要在代码中定义内容类型。我在这里发布我的测试代码。
我在firefox RESTClient中测试了url + header + body并且它们正常工作。 [![在此输入图片说明] [1]] [1] 我确定URL是真的并且在其他地方工作但是在这里我得到错误“打开URL错误”
URL: “https://api.thinger.io/v2/users/ ***”
内容标题:“application / json”
身体:{“in”:true}
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();
错误:
错误:错误#2101:传递给URLVariables.decode()的字符串必须是 包含名称/值对的URL编码查询字符串。
错误$ / throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables()
at ATN_fla :: MainTimeline / ATN()
at ATN_fla :: MainTimeline / frame1()
编辑:
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.");
}
}
输出:
openHandler:[Event type =“open”bubbles = false cancelable = false 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.");
}
}
输出2:
openHandler:[Event type =“open”bubbles = false cancelable = false eventPhase = 2] progressHandler已加载:44总计:44
打开网址“https://api.thinger.io/v2/users/ ***”
时出错httpStatusHandler:[HTTPStatusEvent type =“httpStatus”bubbles = false cancelable = false eventPhase = 2 status = 400 redirected = false responseURL =空]
ioErrorHandler:[IOErrorEvent type =“ioError”bubbles = false cancelable = false eventPhase = 2 text =“错误#2032:流错误.URL: https://api.thinger.io/v2/users/ ***“]
答案 0 :(得分:0)
错误在于解析您提供给URLVariables构造函数的字符串。 URLVariables仅用于构造名称/值对。如果要发送JSON,只需直接设置数据属性:
var postData:Object = { "in": true };
request.data = JSON.stringify( postData );