Heroku上的Express.js上的Cross Origin API Json文件错误

时间:2017-01-23 23:40:09

标签: express heroku cors cross-domain

我在React中有一个Application构建,而Server在Heroku平台上的Express.js中是以下应用程序 http://duberex.herokuapp.com/ 输入有效的邮政编码,如99201

我收到以下错误XMLHttpRequest cannot load https://admin.duberex.com/products/geo_search.json?gps[]=47.66007159999999&gps[]=-117.4316272&searchText=zoot. The 'Access-Control-Allow-Origin' header has a value 'http://localhost:3000' that is not equal to the supplied origin. Origin 'http://duberex.herokuapp.com' is therefore not allowed access.

这是server.js代码     var express = require('express');

// Create our app
var app = express();
const PORT = process.env.PORT || 3000;

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");

   if (req.headers['x-forwarded-proto'] === 'https') {
    res.redirect('http://' + req.hostname + req.url);

     next();
  } else {
    next();
  }

});

app.use(express.static('public'));

app.listen(PORT, function () {
  console.log('Express server is up on port ' + PORT);
});

0 个答案:

没有答案