如何在 Flex 中读取 Django HTTPResponse?

发布于 2024-07-11 19:20:18 字数 227 浏览 7 评论 0原文

我是一个十足的 Flex 菜鸟,所以如果我遗漏了一些明显的东西,我提前道歉。

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

有什么想法吗?

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

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.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

双手揣兜 2024-07-18 19:20:18

进行类似于 then 的操作,

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

在 resultHandler 中

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

}

在 resultHandler 处进行调试,以查看返回的确切内容,确保它是您认为应该返回的内容。

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

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.

小瓶盖 2024-07-18 19:20:18

当它到达客户端时,它只是一个普通的 HTTP 响应,因此请像对待任何其他响应一样对待它

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

烂柯人 2024-07-18 19:20:18

我也是 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
}

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

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.:

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.

む无字情书 2024-07-18 19:20:18

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

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

我们这样做是为了从 java web 服务获取 json。

您只需要首先使用 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);

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

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.

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文