我正在尝试将JSON发布到服务器,但代码不起作用。我找到了一些用ActionScript 3发布JSON的示例,但我需要在代码中定义内容类型。我在这里发布我的测试代码。
我在Firefox Restclient中测试了URL 标头 身体,它们可以工作。[![输入图像描述在这里] [1]] [1]我敢肯定URL是真实的,并且在其他地方工作,但是在这里我有错误"打开URL"
url:" https://api.thinger.io/v2/users/xperydey"
内容标头:"应用程序/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()
flash.net:: urlvariables/decode()
flash.net:: urlvariables()
atn_fla ::维护仪/atn()
atn_fla :: navenimeline/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 casutable = 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 casutable = false EventPhase = 2] ProgressHandler加载:44总计:44
错误打开URL'https://api.thinger.io/v2/users/phydeyby'
httpstatushandler:[httpstatusevent type =" httpstatus" bubbles = false 取消= false eventphase = 2状态= 400重定向= false responseurl = null]
ioErrorhandler:[ioerrorevent type =" ioError" bubbles = false 取消= false eventphase = 2 text ="错误#2032:流错误。url:url: https://api.thinger.io/v2/users/phydeyby"]
错误在于解析您提供给urlvariobles构造函数的字符串。urlvariables实际上仅用于构建名称/值对。如果要发送JSON,请直接设置数据属性:
var postData:Object = { "in": true };
request.data = JSON.stringify( postData );