nodejs
const hostname = '192.168.99.100' //IP
const port = 8080;
//1.导入express模块
const express = require('express');
const bodyParser = require('body-parser');
// cors同源策略
const cors = require('cors')
//2.创建web服务器
const app = express();
// 解决同源
app.use(cors())
// 解决body获取不到
app.use(bodyParser.json());
//设置跨域访问
app.all('*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "content-type");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
})
app.post('/sendCode', (req, res) => {
console.log(req.body.code)
res.statusCode = 200 // 执行成功状态码
res.end('success')
});
app.listen(port, hostname, (error) => {
console.log(`server running http://${hostname}:${port}/`);
})
前端
this.$axios({
method: 'post',
url: 'http://192.168.99.100:8080/sendCode',
data: {
code: 'aaa'
}
}).then()