2

I'm working with an API based on json requests, all the requests are workining but one. The problematic request does not allow cross-domain by default, but using 'Cors' it works. The problem is that when I'm testing the request with javascript using the cors server it works, but when I'm using it with node.js it doesnt.

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

The code that does'nt work:

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

        };        
    });
});

This is the code that works:

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

    }
});

My website does the request to this url: http://gankei-backend.herokuapp.com/activematchbyid/parameters-here

Murilo Bastos
  • 58
  • 1
  • 1
  • 6
  • What do you mean by "I'm testing the request with javascript using the cors server it work"? If you are testing with a utility like POSTMAN, it may be bypassing cross origin and appears to work. see http://stackoverflow.com/questions/25291840/postman-extension-get-a-response-but-my-jquery-request-not – Ryan Gill Feb 17 '15 at 19:05
  • I'm testing by my own, sending requests with my app. – Murilo Bastos Feb 17 '15 at 20:03

2 Answers2

1

I found a Solution, just added this "res.header("Access-Control-Allow-Headers", "x-requested-with, x-requested-by");" to the headers, removed the other ones and removed cors server of the URL. Now its working fine.

The working code:

//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);            
        };        
    });
});
Murilo Bastos
  • 58
  • 1
  • 1
  • 6
-1

Hello try to use this 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>