2014-02-17 32 views
6

考慮下面的僞代碼:如何從HttpRequest.postFormData的catchError的_XMLHttpRequestProgressEvent獲取HTTP響應文本?

import "dart:html"; 

HttpRequest.postFormData(url, data).then((HttpRequest request) { 

    ... 

}).catchError((error) { 

    // How do I get the response text from here? 

}); 

如果Web服務器用400 BAD REQUEST那麼catchError將被調用回覆。但是,錯誤參數的類型爲_XMLHttpRequestProgressEvent,這在Dart的庫中顯然不存在。

那麼,如何從Web服務器發送的400 BAD REQUEST響應中獲得響應文本?

回答

6

看來你的錯誤對象中的目標實際上是你的HttpRequest。

你會發現這個鏈接有用:https://www.dartlang.org/docs/tutorials/forms/#handling-post-requests

你可以這樣做:

import "dart:html"; 

HttpRequest.postFormData(url, data).then((HttpRequest request) { 
    request.onReadyStateChange.listen((response) => /* do sth with response */); 
}).catchError((error) { 
    print(error.target.responseText); // Current target should be you HttpRequest 
}); 
+0

我不確定這是否是正確的,因爲我連漸入「那麼」功能。此外,我認爲你的例子已被破壞,因爲onData(_)中的請求是未知的。 – corgrath

+0

我一直在測試這個,看起來大部分2XX都可以,大多數4XX和5XX都會調用'catchError'。 – corgrath

+0

@corgrath看起來你是對的。我現在也測試過了。您需要將請求傳遞給onData - 這是一個CP失敗。嘗試使用error.currentTarget.responseText – markovuksanovic