先说一下问题,首先这是一个跨域问题,记住是跨域问题
就是当你出现上面这种情况的时候,就是需要你在服务器上配置 header
koa 的处理方式 : 在你的入口文件的 app.use 中间件中加入
ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
这样你在客户端就能收到 koa 服务的发过来的数据了
如果你的服务的是express写的 ,就如下解决
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
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();
})
后端要获取 post 请求的时候最好用 bodyPaser 这个中间件
希望能解决你的问题