php 跨域请求被阻止:同源策略不允许读取 url 上的远程资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36333171/
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-08-26 00:46:40  来源:igfitidea点击:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at url

phpangularjsweb-applicationscross-domainhttprequest

提问by Korte

It's the very famous browser error. I know it has been discussed a lot but I've noticed is a very generic error so I want to present my problem.

这是非常著名的浏览器错误。我知道已经讨论了很多,但我注意到这是一个非常普遍的错误,所以我想提出我的问题。

I am making simple requests (get,post) on a server where I have access. My browsers (chrome, firefox) give me Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at url (Reason: CORS header 'Access-Control-Allow-Origin' does not match 'null').error.

我正在我有权访问的服务器上发出简单的请求(获取、发布)。我的浏览器(chrome、firefox)给我Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at url (Reason: CORS header 'Access-Control-Allow-Origin' does not match 'null').错误。

When I use some of (hacking)plugins I get the responses fine.

当我使用一些(黑客)插件时,我得到了很好的回应。

What I've tried is to add on my back-end (on server):

我试过的是在我的后端(在服务器上)添加:

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

in index.phpfile with no luck. Any other ideas ?

index.php没有运气的文件中。还有其他想法吗?

回答by ddepablo

Try adding

尝试添加

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

回答by Dharmendra Manikpuri

I have solved this issue.

我已经解决了这个问题。

  1. In your config.php add www pre in your domain.com exa.
  1. 在您的 config.php 中,在您的 domain.com exa 中添加 www pre。

// HTTP

// HTTP

define('HTTP_SERVER', 'http://domain name with www/');

// HTTPS

// HTTPS

define('HTTPS_SERVER', 'http://domain name with www/'); 
  1. Add you htaccess file

    RewriteCond %{REQUEST_METHOD} OPTIONS RewriteRule ^(.*)$ $1 [R=200,L]

  1. 添加您的 htaccess 文件

    RewriteCond %{REQUEST_METHOD} OPTIONS RewriteRule ^(.*)$ $1 [R=200,L]

回答by Adam T

There are several ways to do this. One way is the javascript way, which requires a callback and one example can be found here: Loading cross domain html page with AJAX

有几种方法可以做到这一点。一种方式是javascript方式,它需要一个回调,一个例子可以在这里找到:Loading cross domain html page with AJAX

Another way is to utilize PHP's curlfunctionality. Of course there are many ways to do this, but one method that works well for me is to:

另一种方法是利用 PHP 的curl功能。当然,有很多方法可以做到这一点,但对我来说效果很好的一种方法是:

  • Create a standalone php page (can call it "fetch.php" if you like) that has 1 job. That job is to make a curl request to a given URL (your cross-domain url in this case) and echo the data that it gets from the remote site.
  • Change the AJAX URL from the cross-domain URL to the name of file created in previous step.
  • AJAX then knows it's making an HTTP request to a location inside its current domain even though it's getting its data from a cross-domain location.
  • 创建一个包含 1 个作业的独立 php 页面(如果您愿意,可以将其称为“fetch.php”)。这项工作是向给定的 URL(在这种情况下是您的跨域 URL)发出 curl 请求,并回显它从远程站点获取的数据。
  • 将 AJAX URL 从跨域 URL 更改为上一步中创建的文件的名称。
  • AJAX 然后知道它正在向其当前域内的某个位置发出 HTTP 请求,即使它是从跨域位置获取数据。

Hope this helps, Adam

希望这会有所帮助,亚当

回答by Stackhelper

I tried adding a chrome plugin "Allow-Control-Allow-Origin" after several tries with server side changes. Everything worked fine.

在多次尝试服务器端更改后,我尝试添加一个 chrome 插件“Allow-Control-Allow-Origin”。一切正常。