Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
51
rated 0 times [  55] [ 4]  / answers: 1 / hits: 18153  / 9 Years ago, sat, may 2, 2015, 12:00:00

Mobile app where it needs to get access to a JSON file in another server. And its showing cross origin policy blocked. So is there any way to bypass or have the access to the file ?


More From » php

 Answers
1

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>

[#66785] Thursday, April 30, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pariss

Total Points: 255
Total Questions: 99
Total Answers: 117

Location: Hungary
Member since Wed, Nov 9, 2022
2 Years ago
;