跨域缺少必需的请求 header 错误 node.js

Cross domain Missing required request header error with node.js

我正在处理一个基于 json 请求的 API,所有请求都在工作,只有一个。 默认情况下,有问题的请求不允许 cross-domain,但使用 'Cors' 它可以工作。 问题是,当我使用 cors 服务器使用 javascript 测试请求时,它可以工作,但是当我将它与 node.js 一起使用时,它不起作用。

error: 'Missing required request header. Must specify one of: origin,x-requested-with'

无效的代码:

//Active Match
router.get('/activematchbyid/:server/:sid', function(req, res, next) {
    var serverList = {br: 'BR1', na: 'NA1', eune: 'EUNE1', euw: 'EUW1', kr: 'KR1', lan: 'LAN1', las: 'LAS1', oce: 'OCE1', tr: 'TR1', ru: 'RU'};
    var url = 'https://cors-anywhere.herokuapp.com/https://' + req.params.server + '.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/' + serverList[req.params.server] + '/' + req.params.sid + '?api_key=' + apiKey;
    console.log(url);
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    request(url, function(err, resp, body){
        String.prototype.beginsWith = function (string) {
            return(this.indexOf(string) === 0);
        }
        if (body.beginsWith('<html>') || body.beginsWith('Missing')){   
            res.send('error', 200);  
        }else{
            console.log(body);
            body = JSON.parse(body);
            res.send(body);            

        };        
    });
});

这是有效的代码:

var serverList = {br: 'BR1', na: 'NA1', eune: 'EUNE1', euw: 'EUW1', kr: 'KR1', lan: 'LAN1', las: 'LAS1', oce: 'OCE1', tr: 'TR1', ru: 'RU'};
$.ajax({
    url: 'https://cors-anywhere.herokuapp.com/https://' data.server + '.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/' + serverList[data.server] + '/' + data.sid + '?api_key=' + apiKey;,
    success: function(result){

        //code stuff

    }
});

我的网站对此 url 提出请求:http://gankei-backend.herokuapp.com/activematchbyid/parameters-here

我找到了一个解决方案,只是添加了这个 "res.header("Access-Control-Allow-Headers", "x-requested-with, x-requested-by");"到 headers,删除了其他的并删除了 URL 的 cors 服务器。现在可以正常使用了。

工作代码:

//Active Match
router.get('/activematchbyid/:server/:sid', function(req, res, next) {
    var serverList = {br: 'BR1', na: 'NA1', eune: 'EUNE1', euw: 'EUW1', kr: 'KR1', lan: 'LAN1', las: 'LAS1', oce: 'OCE1', tr: 'TR1', ru: 'RU'};
    var url = 'https://' + req.params.server + '.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/' + serverList[req.params.server] + '/' + req.params.sid + '?api_key=' + apiKey;
    res.header("Access-Control-Allow-Headers", "x-requested-with, x-requested-by");
    request(url, function(err, resp, body){
        String.prototype.beginsWith = function (string) {
            return(this.indexOf(string) === 0);
        }
        if (body.beginsWith('<html>') || body.beginsWith('Missing')){   
            res.send(resp.statusCode);  
        }else{
            body = JSON.parse(body);
            res.send(body);            
        };        
    });
});

你好尝试使用这个apihttps://cors.now.sh/

$.ajaxPrefilter( function (options) {
  if (options.crossDomain && jQuery.support.cors) {
    var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
    options.url = http + '//cors.now.sh/' + options.url;
    
  }
});

$.get(
    'http://otomoto.pl/',
     
    function (response) {      
     
      
      
  var $log = $( "#log" ),
      
  html = $.parseHTML( response ),
  title = $(html).find("title").text();
   console.log(response);
 

      $log.append( html );
      title = $(document).find("title").text();
      
    $("#log1").append(title);
      
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="log1">
  <h3>Content:</h3>
</div>
  
  
  
  
 <div style="display:none" id="log">
 </div>