How add headers on json-server?

本小妞迷上赌 提交于 2019-12-01 09:30:12

try adding this in your server.js file, this code precisely will make your server cors enabled and then you will be able to send correct response. Mind about your variable names, and port number rest everything should be identical,

var express = require('express'),
   app = express(),
   port = process.env.PORT || 8080;

app.listen(port);
app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!