How to get the HTTP response text from a HttpRequest.postFormData's catchError's _XMLHttpRequestProgressEvent?

StackOverflow https://stackoverflow.com/questions/21839496

  •  12-10-2022
  •  | 
  •  

Вопрос

Given the following pseudo code:

import "dart:html";

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

    ...

}).catchError((error) {

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

});

If the web server replies with a 400 BAD REQUEST then the catchError will be invoked. However, the error parameter is of the type _XMLHttpRequestProgressEvent which apparently doesn't exist in Dart's library.

So, how do I get the response text from the 400 BAD REQUEST response that was sent from the web server?

Это было полезно?

Решение

It seems like the target in your error object is actually your HttpRequest.

You may find this link helpful: https://www.dartlang.org/docs/tutorials/forms/#handling-post-requests

You could do something like:

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
});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top