简体   繁体   English

Angular 7 / Express - POST 请求的响应体为空

[英]Angular 7 / Express - Null response body on POST request

I'm trying to get/read the response from a POST request made in Angular 7, if I set the API to return "text" everything is OK, but when i make the response a JSON the response body on Angular is null.我正在尝试从 Angular 7 中发出的 POST 请求中获取/读取响应,如果我将 API 设置为返回“文本”,一切正常,但是当我将响应设为 JSON 时,Angular 上的响应正文为空。

If I try the API on Postman I get full response JSON body.如果我在 Postman 上尝试 API,我会得到完整的响应 JSON 正文。

I tried changing response method on Express API (from the simple res.json() to the "old" way of declaring Content-Type and sending it with res.end()), nothing changed.我尝试更改 Express API 上的响应方法(从简单的 res.json() 到声明 Content-Type 并使用 res.end() 发送它的“旧”方式),没有任何改变。

The response code I'm using on backend:我在后端使用的响应代码:

res.status(200).json({
    success: true,
    token: token
})

What I also tried:我也尝试过:

res.writeHead(200, { 'Content-Type': 'application/json' })
var json = JSON.stringify({
    success: true,
    token: token
})
res.end(json)

The service I'm using on Angular:我在 Angular 上使用的服务:

login(username: string, password: string): Observable<any> {
    let body = {username: username, password: password};
    let headers = new HttpHeaders(); 
    headers.set('Content-Type', 'application/json');

    return this.http.post(this.baseUrl + "/login/sign-in", body, {headers: headers, responseType: 'json', observe: 'response'});
  }

The call to that service:对该服务的调用:

this.api.login("admin", "password").subscribe(result => {
    console.log(result);
})

On Postman I get this result:在邮递员我得到这个结果:邮递员结果

On Angular I get this (JSON):在 Angular 上,我得到了这个(JSON):角度结果 - json

On Angular I get this (TEXT):在 Angular 上,我得到了这个(文本):角度结果 - 文本

Edit:编辑:

If I add anything before the JSON on the Express app, the body is no more null:如果我在 Express 应用程序上的 JSON 之前添加任何内容,则正文不再为空:

res.writeHead(200, { 'Content-Type': 'application/json' })
    var json = JSON.stringify({
    success: true,
    token: token
})
res.end('some_char' + json)

The result (of course the response goes in error):结果(当然响应出错):结果

Edit 2:编辑2:

I'm also trying (with no luck) with this simple version of the endpoint:我也在尝试(没有运气)这个简单版本的端点:

const express = require('express')

const app = express()

app.post('/login/sign-in', (req, res) => res.json({ value: 1 }))

app.listen(3000, () => {
    console.log('App running on port 3000.')
})

Solution:解决方案:

It was all a CORS problem, I added this to the backend and everything is working fine:这完全是 CORS 问题,我将其添加到后端,一切正常:

app.use(cors())

Spent a few minutes trying to find out why the body would be empty,花了几分钟试图找出为什么尸体会是空的,

In my case, I had "mode":"no-cors" set in my fetch() options, therefore the returned value from the server would appear as "opaque"就我而言,我在 fetch() 选项中设置了 "mode":"no-cors",因此从服务器返回的值将显示为 "opaque"

redux fetch body is not use with no cors mode redux fetch body 不用于无 cors 模式

I hope this can help !我希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM