我有一个react Component,它向NestJS端点发出发布请求,由于某种原因,我无法提供主体。每次我将一个物体附着到身体上时,我都会在控制台中看到一条错误消息,提示“错误:网络错误”。
如果我仅在axios帖子中提供一个字符串,我将从NestJs服务器获得响应。
如果我和Postman一起尝试,一切都会很好。
React中的请求:
const getImage = async () => {
try {
const result = await Axios.post('http://localhost:8081/abstract/image', {test: 'asd'} )
} catch (e) {
console.log(e)
}
}
NestJS控制器:
@Post('image')
@Header('Content-Type', 'image/png')
@Header('Access-Control-Allow-Origin', '*')
@Header("Access-Control-Allow-Credentials", "true")
@Header('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT')
@Header("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers")
async getImageBuffer(
@Body() completeBody
) {
console.log(completeBody)
}
答案 0 :(得分:0)
如果您使用create-react-app构建您的react应用,请考虑将proxy
添加到package.json文件中,如下所示:
{
"name": "your-awesome-app",
"version": "whatever-version-it-is",
"proxy": "http://localhost:8081"
"dependencies": {... all your dependencies}
"devDependencies": {... all your devDependencies}
}
并像这样更改您的代码:
const getImage = async () => {
try {
const result = await
Axios.post('abstract/image', {test: 'asd'} )
} catch (e) {
console.log(e)
}
}