Express JS: No 'Access-Control-Allow-Origin' header is present on the requested resource

前端 未结 3 737
暗喜
暗喜 2020-12-09 00:02

I have an API running on a server and a front-end client connecting to it to retrieve data. I did some research on the cross domain problem and has it working. However I\'ve

3条回答
  •  孤街浪徒
    2020-12-09 00:09

    Instead of setting the request headers to your express route, Can you try setting it to express instance itself like this,

    var express = require('express');
    var app = express();
    var Assessment = require('../app/models/assessment');
    
    app.use(function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
      next();
    });
    
    app.post('/api/status', function (req, res, next) {
        // your code goes here
    });
    
    module.exports = app;
    

    Hope this helps!

提交回复
热议问题