是这样的,我前端uniapp提交post请求,nodejs后端接收不到参数,一直打印req.body为undefind,不知道错哪了,大锅们帮我看看呗
request的封装:
export const request = (option) => {
option.url = 'http://localhost:3000/api' + option.url
option.method = option.method || "Get"
option.data = option.data || {}
option.header = option.header || {"content-type":"application/x-www-form-urlencoded"}
option.dataType = option.dataType || "json"
return new Promise((res,rej) => {
uni.showLoading({
title: '数据加载中'
})
uni.request({
...option,
success:(val) => {
if(val.code != 200) {
return rej()
}
let data = val.data
res(data)
}
})
uni.hideLoading()
})
}
post请求的发起:
methods: {
login:async function(){
let form = this.form
let obj = {}
let newForm = Object.assign(obj,form)
console.log(newForm);
const post = await request({url:'/login/post',method:'post',data:newForm})
console.log('post',post);
},
},
nodejs入口文件的处理:
const express = require('express')
const app = express()
const port = require('./config/index')
const bodyParser = require('body-parser')
const cors = require('cors')
app.use('/api',require('./router/index.js'))
app.use(bodyParser.urlencoded({extended:false,limit:'100mb'}))
app.use(bodyParser.json({limit:'100mb'}))
// 处理跨域
app.use(cors())
app.listen(port.app.port,() => {
console.log('宠物市场nodejs端运行成功');
})
nodejs‘具体处理post请求的路由文件:
router.post('/post',(req,res) => {
try {
console.log('-------------------',req.body);
let body = req.body
console.log('----body----',body);
pool.getConnection(function(err,connection) {
if(err) {
console.log('/login/post--connection--err',err);
return res.status(500)
}
connection.query(`insert into login values(0,?,?)`,Object.values(body),(error,data) => {
if(error) {
console.log('/login/post--connection--error',error);
return res.status(500)
}
connection.release()
res.send({code:200,msg:'账号创建成功'})
})
})
} catch(err) {
res.status(500).send(err)
}
})
之前我都是这么弄的啊,一段时间不打代码咋就不行啦