node.js - koa ajax 返回数据

 zhouib8oevlap 发布于 2022-10-31 23:12

想问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正在啃, 但项目来的急啊, 先求方法解决一下

2 个回答
  • 服务端没有开跨域资源共享吧
    npm install kcors --save
    然后把这句写在路由前面
    app.use(require('kcors')())

    =============
    Koa在服务端设置返回头的方式 ctx.set('header mame', 'header content')

    2022-10-31 23:13 回答
  • 看报错好像是跨域请求,响应头里面缺少必要字段,设置如下头

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

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

    2022-10-31 23:13 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有