我的代码:
react.js
let lsStr = ''
lsStr = `https://192.168.0.1/basic/code02/exlup02`
if (window.confirm(message)) {
fetch(lsStr,
{ method: "POST",
body: JSON.stringify({"Data": "MY DATA"}),
headers:{
'Content-Type':'application/x-www-form-urlencoded'
}
})
.then(function(res){ return res.json(); })
.then(data =>console.log(data));
2.server.js
var express = require('express')
var router = express.Router();
const cors = require('cors')
var pool = require('../db')
router.post('/code02/exlup02', function(req, res, next) {
console.log('post page')
let lsModnm = req.body;
if(!lsModnm){
return res.status(400).send({ error:true, message: 'Please provide excel' });
}else{
console.log(req.body)
}
});
当前:console.log(req.body)记录{}
return message : Please provide excel!!
所需:console.log(必需的主体)记录“我的数据”
不能用路由器解决吗?
我该如何解决问题?
答案 0 :(得分:1)
解决了问题
1.react.js
固定
headers:{
'Accept': 'application/json',
'Content-Type': 'application/json'
}
2.server.js
添加
let bodyParser = require('body-parser');
router.use(bodyParser.json());
consol.log
post page
[ { modnm: 'test1', remark: 1, useyn: 're1' },
{ modnm: 'test2', remark: 1, useyn: 're2' },
{ modnm: 'test3', remark: 1, useyn: 're3' },
{ modnm: 'test4', remark: 1, useyn: 're4' },
{ modnm: 'test5', remark: 1, useyn: 're5' } ]