簡體   English   中英

使用node.js跨域缺少必需的請求標頭錯誤

[英]Cross domain Missing required request header error with node.js

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

我找到了一個解決方案,只需添加此“ res.header(“ Access-Control-Allow-Headers”,“ x-requested-with,x-requested-by”);“ 到標題,然后刪除其他網址,並刪除網址的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);            
        };        
    });
});

您好嘗試使用此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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM