getting a 200 status but still an error when using

2019-08-29 03:26发布

问题:

I have 2 POSTs that are running through a promise, but regardless of which one is on the inside, they are hitting the catch and not executing the inside POST.

I have the following code:

this.http.post(medurl, datamed)
    .toPromise()
    .then(response => {
      console.log('Post Success!');
      console.log('Reponse: ' + response);

      if (response != 0){
        this.http.post(scripturl, datascript)
            .toPromise()
            .then(response => {
            console.log('Post Success!');
            console.log('Reponse: ' + response);
            })
            .catch(error => {
            console.log('Post Error!');
            console.log('Reponse: ' + typeof(error));
            });
       }

    })
    .catch(error => {
      console.log('Post Error!');
      console.log('Reponse: ' + typeof(error));
    });

I'm not sure what the issue is, because I've double-triple checked the fields for the POSTs, and they're working just fine in a different context, plus the response is a 200.

I'm not sure what other details to add here, but if you need more info let me know.

回答1:

I had a similar issue where I was receiving a 200 status response but would still throw an error. I had to set the responseType attribute on the options parameter of HttpClient's post method.

this.http.post(medurl, datamed, { responseType: 'text' })
  .toPromise()
  .then(response => {

    ....

  })
  .catch(error => {
    console.log('Post Error!');
    console.log('Reponse: ' + typeof(error));
  });

https://angular.io/guide/http#requesting-non-json-data