首页 > 解决方案 > 尽管请求处理成功,但从发布请求中获得空响应

问题描述

我正在使用post请求,但通过请求处理成功httpClient获得响应。null

serviceClass.ts文件

this.httpOptions = {
  headers: new HttpHeaders(
    { 
      'Content-Type': 'application/json; charset=utf-8',
      'tenant-code' : 'VdoFWiHL8RFR8fRGNjfZI=',
      'Authorization': 'Basic ' + btoa('pass:username')
    })
}

public Reprocess(ObjProduct) {
var Jobj=JSON.stringify(ObjProduct);
return this._http.post(this.ReprocessUrl,Jobj,this.httpOptions)
}

当我在 Component 中调用上述方法时,我得到null了 API 的响应。

组件代码

var op = this.objService.reprocess(this.reprobj);
console.log("output: ", op);

op是完全无法理解的,它正在显示_scaler=false等。我怎样才能获得服务呼叫的正确状态?

编辑 1:当我向邮递员提出相同的请求时,获取 status Ok 200

编辑2:下面的代码也给出了null结果(根据@Spart_Fountain的回答)

var op= this.restApi.Reprocess(this.reprobj).subscribe((data:any) => {
console.log("data "+ data);    
});

邮递员标题截图

在此处输入图像描述

标签: angulartypescripthttprequest

解决方案


调用时收到“奇怪”响应的原因this.objService.reprocess(this.reprobj);是此方法将返回一个Subscription对象。

详细说明:该方法reprocess返回对可观察对象的订阅,因为该方法subscribe()是在return语句本身内部调用的。你宁愿做的是只返回 observable 并在reprocess方法之外订阅它:

public reprocess(objProduct) {
  var objParam = JSON.stringify(objProduct);
  return this._http.post(this.url, objParam, this.httpOptions);
}

var op = this.objService.reprocess(this.reprobj).subscribe(resp => {
  console.log("resp from api service: " + resp);
});

推荐阅读