[英]Express endpoint works in Postman but not in my app
I am working on an application with both a front-end and a back-end.我正在开发具有前端和后端的应用程序。 The front uses Angular, and the back Node.js (Express).
前面使用Angular,后面使用Node.js(Express)。 They communicate with a Rest API.
它们与 Rest API 通信。
When I request this endpoint with Postman ou directly with my web browser, it works and I get the right result.当我直接使用我的 Web 浏览器向 Postman 请求此端点时,它可以工作并且得到正确的结果。
However, when I am requesting it since one component of my app, it just doesn't work.但是,当我因为我的应用程序的一个组件而请求它时,它就不起作用。
The call is here电话在这里
console.log("Call GETSAVE");
this.rest.getSave(type, file).subscribe((data) => {
console.log(data);
});
Then, in my service然后,在我的服务中
getSave(type: enumType, path: string) {
console.log(this.url + 'saves/' + type + path);
return this.http.get(this.url + 'saves/' + type + path);
}
with HTTP being an instance of HttpClient (injected in the constructor). HTTP 是 HttpClient 的一个实例(在构造函数中注入)。
All endpoints of this service work and I call them in the same way as above, but for this endpoint, the subscribe just doesn't work.此服务的所有端点都有效,我以与上述相同的方式调用它们,但对于此端点,订阅不起作用。 I never enter the subscribe.
我从来没有进入订阅。
I finally found the answer, and it's quite simple.我终于找到了答案,而且很简单。
By default, HTTPClient will parse data as a JSON.默认情况下,HTTPClient 会将数据解析为 JSON。 In my case, it was not a JSON but a text.
就我而言,它不是 JSON,而是文本。 In consequence, there was an error because it couldn't parse the text as a JSON.
结果,出现了错误,因为它无法将文本解析为 JSON。
To fix this issue, you need to add a parameter in the request with a "responseType".要解决此问题,您需要在请求中添加一个带有“responseType”的参数。
In my case :就我而言:
getSave(type: enumType, path: string) {
console.log(this.url + 'saves/' + type + path);
return this.http.get(this.url + 'saves/' + type + path, {
'responseType':'text'
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.