我使用 axios 发送的数据在配置中返回,而不是数据
My sent data using axios is returned in config, not data
我正在 NextJS NodeJS 和 Express 上构建我的 Web 应用程序,我在 运行 本地主机上有两台服务器,3000 用于 next,9000 用于 express。
我有一个带有两个输入字段的表单,我正在使用 axios post 将状态发送到带有数据的 url,在服务器端我正在接收该请求并发回收到的数据与响应相同。
我从服务器收到数据响应:成功,我的数据在 config.data
为什么我的数据在配置数据中,我怎样才能从这个 JSON 中取出它,这样我就可以将它传递给一个变量。
至于从 config.data 中获取数据,我已经尝试了 for 循环,但它们要么将 56 个数字的 56 个元素推入空数组,要么什么都不做。
客户端:
state = {
bank_name: '',
account_number: ''
}
...
onSubmit = (e) => {
e.preventDefault()
axios.post('http://localhost:9000/api/bank', {
bankName: this.state.bank_name,
accNumber: this.state.account_number
})
.then(res => {
console.log(res)
}).catch(err => console.log(err))
}
服务器端:
router.post('/', (req, res) => {
const {reqData} = req;
res.send(reqData);
})
来自客户端的控制台日志 (console.log(res)
):
{
config: {
url: "http://localhost:9000/api/bank",
method: "post",
data: '{"bankName":"some new bank","accNumber":"39276542934235"}'
},
data: "success",
headers: "...",
request: "...",
status: 200,
statusText: "OK",
__proto__: Object
}
...
当我定位 res.config.data.bankName
时,我得到 undefined
.
我认为这与服务器响应原样有关,或者首先没有解析服务器接收到的数据,或者是由于承诺。
任何意见都会有帮助,谢谢
那 res.config.data
是 string
所以先解析它 JSON.parse(res.config.data)
然后访问 bankName
.
此外,您必须在 express
末尾使用 body-parser
。因此 post
数据驻留在 req.body
中,您应该将其发回而不是整个 req
IMO。
快递:
router.post('/', (req, res) => {
const reqData = req.body;
return res.send(reqData);
});
axios:(返回数据应该在res.data
)
axios.post('http://localhost:9000/api/bank', {
bankName: this.state.bank_name,
accNumber: this.state.account_number
})
.then(res => {
console.log(res.data);
}).catch(err => console.log(err))
}
我正在 NextJS NodeJS 和 Express 上构建我的 Web 应用程序,我在 运行 本地主机上有两台服务器,3000 用于 next,9000 用于 express。
我有一个带有两个输入字段的表单,我正在使用 axios post 将状态发送到带有数据的 url,在服务器端我正在接收该请求并发回收到的数据与响应相同。
我从服务器收到数据响应:成功,我的数据在 config.data
为什么我的数据在配置数据中,我怎样才能从这个 JSON 中取出它,这样我就可以将它传递给一个变量。
至于从 config.data 中获取数据,我已经尝试了 for 循环,但它们要么将 56 个数字的 56 个元素推入空数组,要么什么都不做。
客户端:
state = {
bank_name: '',
account_number: ''
}
...
onSubmit = (e) => {
e.preventDefault()
axios.post('http://localhost:9000/api/bank', {
bankName: this.state.bank_name,
accNumber: this.state.account_number
})
.then(res => {
console.log(res)
}).catch(err => console.log(err))
}
服务器端:
router.post('/', (req, res) => {
const {reqData} = req;
res.send(reqData);
})
来自客户端的控制台日志 (console.log(res)
):
{
config: {
url: "http://localhost:9000/api/bank",
method: "post",
data: '{"bankName":"some new bank","accNumber":"39276542934235"}'
},
data: "success",
headers: "...",
request: "...",
status: 200,
statusText: "OK",
__proto__: Object
}
...
当我定位 res.config.data.bankName
时,我得到 undefined
.
我认为这与服务器响应原样有关,或者首先没有解析服务器接收到的数据,或者是由于承诺。
任何意见都会有帮助,谢谢
那 res.config.data
是 string
所以先解析它 JSON.parse(res.config.data)
然后访问 bankName
.
此外,您必须在 express
末尾使用 body-parser
。因此 post
数据驻留在 req.body
中,您应该将其发回而不是整个 req
IMO。
快递:
router.post('/', (req, res) => {
const reqData = req.body;
return res.send(reqData);
});
axios:(返回数据应该在res.data
)
axios.post('http://localhost:9000/api/bank', {
bankName: this.state.bank_name,
accNumber: this.state.account_number
})
.then(res => {
console.log(res.data);
}).catch(err => console.log(err))
}