简体   繁体   English

Express 端点在 Postman 中有效,但在我的应用中无效

[英]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.

相关问题 Angular: JWT 令牌在 Postman 上可以正常工作,但在我的应用程序中却不行 - Angular: JWT Token works fine on Postman but not in my app Express-API POST 在 Postman 中工作,但在 AJAX 中无效 - Express-API POST works in Postman but not with AJAX Express + Passport session 在 Postman 中有效,但在浏览器中无效 - Express + Passport session works in Postman but not in browser 为什么Postman会在我的React应用程序中收到快速会话cookie而不是我的帖子请求 - Why Postman recive the express session cookie but not my post request in my React app 检查令牌是否适用于端点-Node / Express - Check if token works with endpoint - Node/Express 为什么 Postman 给我在 Express 应用程序中的路线的错误“无法发布”? - Why is Postman giving me the error 'Cannot POST' for my routes in express app? 端点或路由不准确? (axios 调用在代码中有效,但在邮递员上无效) - Endpoint or route inaccurate? (axios call works in code but not on postman) 我的 express api 在邮递员中工作,但在从 react 调用时不起作用。它在尝试获取资源时给出“TypeError: NetworkError”。 - my express api works in postman but not when calling from react.It gives "TypeError: NetworkError when attempting to fetch resource." 为 SAP CAP 应用定义自定义 Express 端点 - Defining custom Express endpoint for SAP CAP app Postman 获取请求有效,但在浏览器中无效 - react/mongoose/express - Postman Get request works but not in browser- react/mongoose/express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM