node.js - koa ajax 返回数据

查看:166
本文介绍了node.js - koa ajax 返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

想问koa ajax是怎么返回数据的?
之前是使用express可以用res.writeHead设置头, res.write设置返回内容, 但是koa没有了这些方法, 要怎么返回呢?

我直接this.body = 并没有效果

Fetch API cannot load http://192.168.33.1:8888/indie/song/1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8888' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

初学koa不是很懂, es6正在啃, 但项目来的急啊, 先求方法解决一下

解决方案

看报错好像是跨域请求,响应头里面缺少必要字段,设置如下头

this.set({
  'Access-Control-Allow-Origin': '*'
});

看着你好像用时fetch,记得别忘记设置fetch的mode成cors

这篇关于node.js - koa ajax 返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆