gpt4 book ai didi

javascript - Post 表单请求对象为 'object Object'

转载 作者:行者123 更新时间:2023-12-04 01:36:34 24 4
gpt4 key购买 nike

刚刚进入 Node 并且我在这里阅读了一些关于 SO 的问题,但是,请求正文仍然是 { 'object Object' : ''}
服务器代码是:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.set('port', 1111);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/', (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});

app.get('/', (req, res) => {
res.send('this is a normal response');
});

app.post('/d*', (req, res) => {
const reqBody = req.body;
console.log(req.body); // console => {`object Object` : ''}
res.send(reqBody);
});
app.listen(app.get('port'), () => console.log('Server instance running on http://localhost:1111'));

客户端函数是一个简单的“获取请求”:
const postRegistrationForm = (userDetails, dispatch) => {
const url = 'http://localhost:1111/d/register';
const config = {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: userDetails
};
fetch(url, config)
.then(data => data.json())
.then(res => console.log('rezzz is...', res));
};

最佳答案

在使用 fetch 发送之前,您需要对任何主体/对象进行字符串化。

尝试使用此配置进行获取:

const config = {
method: 'POST',
headers: {
'Accept': 'application/json'
'Content-Type': 'application/json'
},
body: JSON.stringify(userDetails)
};

关于javascript - Post 表单请求对象为 'object Object',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49455639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com