反应:
const formData = [{name:'max' , age:35},{name:'katy' , age:27}]
axios.post('http://localhost:5000/api/remarkety', {formData})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
节点:
app.post('/api/point', (req, res) => {
res.send(JSON.parse(JSON.stringify(req.body.data)));
});
我正在尝试将与对象进行反应的后处理请求发送到Nodejs,但出现SyntaxError:JSON中位置0处的意外令牌u
答案 0 :(得分:0)
在router.js文件中(如果没有,请先进行npm install
express和bodyparser)用express进行操作:
const express = require("express");
const router = express.Router();
router.post('/api/point', (req, res) => {
const newData = req.body.data
newData
.save()
.then(data => {res.json(data)})
.catch(err => {
res.status(404).({sucess:false})})
});
module.exports = router;
并在您的server.js文件中:
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use("/api/point", require(" //your router.js files path "));
对于您的axios,应按以下方式设计:
const formData = [{name:'max' , age:35},{name:'katy' , age:27}]
const config = {
headers: {
'Content-Type': 'application/json'
}
};
axios.post('http://localhost:5000/api/remarkety', formData, config)
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});