【问题标题】:Cross domain Missing required request header error with node.js跨域缺少 node.js 所需的请求标头错误
【发布时间】:2015-04-18 13:18:15
【问题描述】:

我正在使用基于 json 请求的 API,所有请求都在工作,但只有一个。 默认情况下,有问题的请求不允许跨域,但使用“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

    }
});

我的网站向这个网址发出请求:http://gankei-backend.herokuapp.com/activematchbyid/parameters-here

【问题讨论】:

  • “我正在使用它工作的 cors 服务器使用 javascript 测试请求”是什么意思?如果您正在使用 POSTMAN 之类的实用程序进行测试,它可能会绕过跨源并且似乎可以工作。见stackoverflow.com/questions/25291840/…
  • 我自己测试,用我的应用发送请求。

标签: javascript node.js api heroku cors


【解决方案1】:

我找到了一个解决方案,只是添加了这个“res.header(“Access-Control-Allow-Headers”,“x-requested-with,x-requested-by”);”到标题,删除其他标题并删除 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);            
        };        
    });
});

【讨论】:

    【解决方案2】:

    你好尝试使用这个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>

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 1970-01-01
      • 2017-08-01
      • 2018-02-03
      • 2020-03-24
      • 2021-10-03
      • 2016-12-21
      • 2016-04-02
      • 2013-05-23
      相关资源
      最近更新 更多