获得200状态,但在角度4中使用POST时仍然出现错误 [英] getting a 200 status but still an error when using POST in angular 4

查看:86
本文介绍了获得200状态,但在角度4中使用POST时仍然出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个POST都通过一个Promise运行,但是无论内部是哪个,它们都被抓住了,并且没有执行内部POST.

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.

我有以下代码:

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));
    });

我不确定是什么问题,因为我已经三重检查了POST的字段,并且它们在不同的上下文中也可以正常工作,并且响应为200.

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.

推荐答案

我有一个类似的问题,我收到200状态响应,但仍然会抛出错误.我必须在HttpClient的post方法的options参数上设置responseType属性.

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

这篇关于获得200状态,但在角度4中使用POST时仍然出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆