How to Bypass Access-Control-Allow-Origin

how to bypass Access-Control-Allow-Origin?

Put this on top of retrieve.php:

header('Access-Control-Allow-Origin: *');

Note that this effectively disables CORS protection, and leaves your users exposed to attack. If you're not completely certain that you need to allow all origins, you should lock this down to a more specific origin:

header('Access-Control-Allow-Origin: https://www.example.com');

Please refer to following stack answer for better understanding of Access-Control-Allow-Origin

https://stackoverflow.com/a/10636765/413670

how to bypass Access-Control-Allow-Origin for an API post request

i have found solution ,
its not the right way , but it can be better than installing the browser plugin to pybass CORS ,
solution is use a proxy , something like this :

         var proxy = 'https://cors-anywhere.herokuapp.com/';
var url = "api url";

// GET SESSION ID
$.ajax({
url: proxy + url,

How to bypass Cross origin policy

As already answered, you want a simple php proxy script.

This way your server grabs the json file and you simply access your server from client side. . That way javascript is only dealing with the same domain.

<?php
header('Content-Type: application/json');
echo file_get_contents('http://example.com/data.json');
?>

Proxy.php

<?php
header('Content-Type: application/json');
echo file_get_contents('http://example.com/'.$_REQUEST['file']);
?>

Another way also would be to send all of the request headers as a query string, this could be post/get as well


if (isset($_REQUEST['query'])) {
$sQuery = http_build_query($_REQUEST);
header('Content-Type: application/json');
echo file_get_contents('https://www.example.com?'.$sQuery);
exit;
}

?>

Using the second example you can try something like http://localhost/proxy.php?file=somefile.json

HTACCESS METHOD

Refer the following page about using a htaccess file on the server htaccess Access-Control-Allow-Origin

<FilesMatch ".(json|js|jsn)">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>


Related Topics



Leave a reply



Submit