我正在嘗試使用 axios 將資料物件發送到后端...問題是我也應該在后端使用 axios 嗎?我似乎無法獲得價值。
axios({
method: 'post',
url: '/encrypt',
data: {
firstName: 'Fred',
lastName: 'Flintstone',
},
//headers: {'Authorization': 'Bearer ...'}
});
app.post('/encrypt', (request, response) => {
console.log(request.body, 'Request................');
});
uj5u.com熱心網友回復:
確保包含正文決議器。
const express = require('express')
const app = express()
const port = 3000
const bodyParser = require('body-parser')
// create application/json parser
const jsonParser = bodyParser.json()
app.post('/encrypt', jsonParser, (req, res) => {
console.log(req.body);
res.send(req.body);
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
和客戶端
const axios = require('axios').default;
axios({
method: 'post',
url: 'http://localhost:3000/encrypt',
data: {
firstName: 'Fred',
lastName: 'Flintstone',
},
//headers: {'Authorization': 'Bearer ...'}
});
uj5u.com熱心網友回復:
嘗試修復您的代碼并根據語法撰寫。Axios 回傳一個承諾。
axios.post('/encrypt', {
firstName: 'Fred',
lastName: 'Flintstone }).then(function (response) {
console.log(response)}).catch(function (error) {
console.log(error)});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/437176.html
