javascript 如何绕过跨源策略

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29997705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:27:33  来源:igfitidea点击:

How to bypass Cross origin policy

javascriptphpsame-origin-policy

提问by Piash Hassan

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 ?

需要访问另一台服务器中的 JSON 文件的移动应用程序。并且其显示的跨源策略被阻止。那么有没有办法绕过或访问该文件?

回答by Angry 84

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

正如已经回答的那样,您需要一个简单的 php 代理脚本。

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.

这样您的服务器获取 json 文件,您只需从客户端访问您的服务器。. 这样 javascript 只处理同一个域。

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

Proxy.php

代理.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

另一种方法是将所有请求标头作为查询字符串发送,这也可以是 post/get

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

使用第二个示例,您可以尝试类似的操作 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

请参阅以下有关在服务器上使用 htaccess 文件的页面htaccess Access-Control-Allow-Origin

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

回答by Misunderstood

You categorized this under PHP. You would do well to get the JSON with PHP then use PHP to create the data required by the JS.

您将其归类到 PHP 下。您最好使用 PHP 获取 JSON,然后使用 PHP 创建 JS 所需的数据。

Without more information regarding your app, I am very limited here.

没有关于您的应用程序的更多信息,我在这里非常有限。

This is a very typical PHP example geting json data into JavaScript:

这是一个非常典型的将 json 数据导入 JavaScript 的 PHP 示例:

$json = json_decode(file_get_contents('http://example.com/data.jsn'),true);
$JS = 'var data = ';
foreach ($json as $key => $value){
  $JS .= "[$key,$value],"
}
$JS = substr($JS,0,-1) . ';';  // remove trailing comma, add semicolon



echo <<<EOT
<script type="text/javascript">//<![CDATA[
$JS
//]]>
</script>
EOT;

回答by shadab

Use header function. check out this link how to bypass Access-Control-Allow-Origin?

使用标题功能。查看此链接如何绕过 Access-Control-Allow-Origin?

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