跨域缺少node.js所需的请求标头错误

时间:2015-02-17 18:20:18

标签: javascript node.js api heroku cors

我正在使用基于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

2 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,只是添加了这个“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);            
        };        
    });
});

答案 1 :(得分:-1)

您好尝试使用此api https://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>