angular - 尽管请求处理成功,但从发布请求中获得空响应
问题描述
我正在使用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);
});
邮递员标题截图
解决方案
调用时收到“奇怪”响应的原因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);
});
推荐阅读
- html - 如果显示不是内联块,为什么行高不起作用?
- java - 在 java 中使用 SHA-256 散列十六进制数
- c# - 从 C# 中的库返回 List(T)
- azure-bicep - 二头肌如何同时部署App Service + Certificate + hostbinding?
- java - 如何创建 SimpleWeightedGraph
来自生成树 - r - 在 R 中创建一个迭代循环以读取文件夹中的所有 Excel 文件,执行相同的功能,并导出为新文件
- python - Big Query 客户端响应的模拟答案
- sql - Oracle时间戳sql
- visual-studio-code - 有没有办法在 chromebook 上使用 VSCode?
- python - Flask restful 正在做 GET 而不是 POST