如何在Flex中读取Django HTTPResponse?

时间:2022-05-13 22:53:00

I'm a complete Flex noob, so I apologize in advance if I'm missing something obvious.

我是一个完整的Flex noob,所以如果我遗漏了一些明显的东西,我会提前道歉。

I wrote a fairly simple file uploader in Flex, which calls my Django back-end via URLRequest (the FileReference object handles the upload). My upload works as intended and I have Django return a HTTPResponse object. As such, I'd like to read the contents of the HTTPResponse object.

我在Flex中编写了一个相当简单的文件上传器,它通过URLRequest调用我的Django后端(FileReference对象处理上传)。我的上传按预期工作,我让Django返回一个HTTPResponse对象。因此,我想阅读HTTPResponse对象的内容。

Any thoughts?

4 个解决方案

#1


something along the lines of

一些东西

<mx:HTTPService id="myHTTPRequest" 
    url="{whatever your url request is}"
    result="resultHandler(event)" 
    fault="faultHandler(event)"
    showBusyCursor="true" 
    resultFormat="object"> 

then inside the resultHandler something like this

然后在resultHandler里面这样的东西

private function resultHandler (event : ResultEvent) : void {
    var obj : Object = event.result;
    //do something with returned object

}

Debug at the point of the resultHandler to see exaclty whats being returned, make sure its what you think should be getting returned.

在resultHandler点调试以查看返回的exaclty,确保它应该返回它的内容。

#2


By the time it gets to the client it's just a normal HTTP response so treat it like any other response

当它到达客户端时,它只是一个正常的HTTP响应,所以像对待任何其他响应一样对待它

#3


I am also new to flex and I ran in the same problem when doing an upload to a Java Rest backend, I solved it using the DateEvent on the FileReference. To get the response data use something like this.:

我也是flex的新手,在上传到Java Rest后端时遇到了同样的问题,我使用FileReference上的DateEvent解决了它。要获取响应数据,请使用以下内容:

var fileRef:FileReference = new FileReference();
fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, responseHandler);
var request:URLRequest = new URLRequest("yourUrl");
fileRef.upload(request, "fileData"); 

private function responseHandler(event:DataEvent):void {
    var response:XML = new XML(event.data); 
//Note the DataEvent: this is the event that holds the response. 
//I sent back data as xml
}

Your response should always be a successful HTTP status code (200), if your backend sends status 500 codes it will not trigger the DateEvent. Server errors can still be caught with a HTTPStatusEvent, but then you don't have access to the response.

您的响应应始终是成功的HTTP状态代码(200),如果您的后端发送状态500代码,它将不会触发DateEvent。仍然可以使用HTTPStatusEvent捕获服务器错误,但是您无权访问响应。

#4


you can access the response like so in your onComplete event handler:

您可以在onComplete事件处理程序中访问响应:

private function saveCompleteHandler(event:Event):void {
    var loader:URLLoader = event.currentTarget as URLLoader;
    trace("saveCompleteHandler - event returned:" + loader.data as String);
}

we do this this to get json fron a java web service.

我们这样做是为了让json从一个java Web服务。

you just need to use a URLLoader to load the URLRequest in the first place:

你只需要使用URLLoader来加载URLRequest:

var loader:URLLoader = new URLLoader();
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, statusHandler, 10000);
loader.addEventListener(IOErrorEvent.IO_ERROR, saveErrorHandler, 10000);
loader.addEventListener(Event.COMPLETE, saveCompleteHandler, 10000);

var request:URLRequest = new URLRequest("http:/whereverer");
request.method = URLRequestMethod.GET;
loader.load(request);

#1


something along the lines of

一些东西

<mx:HTTPService id="myHTTPRequest" 
    url="{whatever your url request is}"
    result="resultHandler(event)" 
    fault="faultHandler(event)"
    showBusyCursor="true" 
    resultFormat="object"> 

then inside the resultHandler something like this

然后在resultHandler里面这样的东西

private function resultHandler (event : ResultEvent) : void {
    var obj : Object = event.result;
    //do something with returned object

}

Debug at the point of the resultHandler to see exaclty whats being returned, make sure its what you think should be getting returned.

在resultHandler点调试以查看返回的exaclty,确保它应该返回它的内容。

#2


By the time it gets to the client it's just a normal HTTP response so treat it like any other response

当它到达客户端时,它只是一个正常的HTTP响应,所以像对待任何其他响应一样对待它

#3


I am also new to flex and I ran in the same problem when doing an upload to a Java Rest backend, I solved it using the DateEvent on the FileReference. To get the response data use something like this.:

我也是flex的新手,在上传到Java Rest后端时遇到了同样的问题,我使用FileReference上的DateEvent解决了它。要获取响应数据,请使用以下内容:

var fileRef:FileReference = new FileReference();
fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, responseHandler);
var request:URLRequest = new URLRequest("yourUrl");
fileRef.upload(request, "fileData"); 

private function responseHandler(event:DataEvent):void {
    var response:XML = new XML(event.data); 
//Note the DataEvent: this is the event that holds the response. 
//I sent back data as xml
}

Your response should always be a successful HTTP status code (200), if your backend sends status 500 codes it will not trigger the DateEvent. Server errors can still be caught with a HTTPStatusEvent, but then you don't have access to the response.

您的响应应始终是成功的HTTP状态代码(200),如果您的后端发送状态500代码,它将不会触发DateEvent。仍然可以使用HTTPStatusEvent捕获服务器错误,但是您无权访问响应。

#4


you can access the response like so in your onComplete event handler:

您可以在onComplete事件处理程序中访问响应:

private function saveCompleteHandler(event:Event):void {
    var loader:URLLoader = event.currentTarget as URLLoader;
    trace("saveCompleteHandler - event returned:" + loader.data as String);
}

we do this this to get json fron a java web service.

我们这样做是为了让json从一个java Web服务。

you just need to use a URLLoader to load the URLRequest in the first place:

你只需要使用URLLoader来加载URLRequest:

var loader:URLLoader = new URLLoader();
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, statusHandler, 10000);
loader.addEventListener(IOErrorEvent.IO_ERROR, saveErrorHandler, 10000);
loader.addEventListener(Event.COMPLETE, saveCompleteHandler, 10000);

var request:URLRequest = new URLRequest("http:/whereverer");
request.method = URLRequestMethod.GET;
loader.load(request);