how to bypass Access-Control-Allow-Origin?

前端 未结 6 1971
闹比i
闹比i 2020-11-22 13:55

I\'m doing a ajax call to my own server on a platform which they set prevent these ajax calls (but I need it to fetch the data from my server to display retrieved data from

6条回答
  •  礼貌的吻别
    2020-11-22 14:30

    Warning, Chrome (and other browsers) will complain that multiple ACAO headers are set if you follow some of the other answers.

    The error will be something like XMLHttpRequest cannot load ____. The 'Access-Control-Allow-Origin' header contains multiple values '____, ____, ____', but only one is allowed. Origin '____' is therefore not allowed access.

    Try this:

    $http_origin = $_SERVER['HTTP_ORIGIN'];
    
    $allowed_domains = array(
      'http://domain1.com',
      'http://domain2.com',
    );
    
    if (in_array($http_origin, $allowed_domains))
    {  
        header("Access-Control-Allow-Origin: $http_origin");
    }
    

提交回复
热议问题