我使用ngResource在亚马逊Web服务上调用REST API得到这个错误:

XMLHttpRequest无法加载 http://server.apiurl.com: 8000 / s /登录吗? =登录facebook。应对 飞行前请求未通过门禁检查:否 'Access-Control-Allow-Origin'头出现在被请求的对象上 资源。因此,来源“http://localhost”不允许访问。 错误405

服务:

socialMarkt.factory('loginService', ['$resource', function ($resource) {
    var apiAddress = "http://server.apiurl.com:8000/s/login/";
    return $resource(apiAddress, {
        login: "facebook",
        access_token: "@access_token",
        facebook_id: "@facebook_id"
    }, {
        getUser: {
            method: 'POST'
        }
    });
}]);

控制器:

[...]
loginService.getUser(JSON.stringify(fbObj)),
    function (data) {
        console.log(data);
    },
    function (result) {
        console.error('Error', result.status);
    }
[...]

我用Chrome浏览器。为了解决这个问题,我还能做些什么?

我甚至将服务器配置为接受来自源localhost的头文件。


当前回答

解决这个问题很简单,只需要几个步骤,不用担心任何事情。

请遵循以下步骤来解决它。

open (https://www.npmjs.com/package/cors#enabling-cors-pre-flight) go to installation and copy the command npm install cors to install via Node.js in a terminal go to Simple Usage (Enable All CORS Requests) by scrolling. Then copy and paste the complete declaration in your project and run it...that will work for sure... copy the comment code and paste in your app.js or any other project and give a try...this will work. This will unlock every cross origin resource sharing...so we can switch between servers for your use

其他回答

在我的Apache VirtualHost配置文件中,我添加了以下几行:

Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"

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

“对飞行前请求的响应没有通过访问控制检查”正是问题所在:

在发出实际的GET请求之前,浏览器会检查服务是否为CORS正确配置。这是通过检查服务是否接受实际请求将使用的方法和头来完成的。因此,允许从不同的来源访问服务是不够的,还必须满足其他必要条件。

将报头设置为

Header always set Access-Control-Allow-Origin: www.example.com 
Header always set Access-Control-Allow-Methods: GET, POST, PUT, PATCH, POST, DELETE, OPTIONS 
Header always set Access-Control-Allow-Headers: Content-Type #etc...

这还不够。你必须添加一个重写规则:

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

飞行前的一个伟大的读响应没有HTTP ok状态。

使用API网关中的CORS选项,我使用了上面所示的以下设置。

另外,请注意,函数必须返回HTTP状态200以响应OPTIONS请求,否则CORS也会失败。

要修复Node.js应用程序中的跨源请求问题:

npm i cors

然后简单地将下面的代码添加到app.js文件中:

let cors = require('cors')
app.use(cors())

我认为在Chrome中禁用CORS不是一个好方法,因为如果你在Ionic中使用它,在移动版本中肯定会再次出现这个问题。

所以最好修改后端。

首先,在header中,需要设置-

标题(“Access-Control-Allow-Origin: *”); header(' header set Access-Control-Allow-Headers: "Origin, X-Requested-With, Content-Type, Accept"');

如果API的行为是GET和POST,那么也设置在你的头-

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {if (收取($ _SERVER [' HTTP_ACCESS_CONTROL_REQUEST_METHOD '])) header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); 如果收取($ _SERVER [' HTTP_ACCESS_CONTROL_REQUEST_HEADERS '])) 标题(“Access-Control-Allow-Headers: {$ _SERVER [' HTTP_ACCESS_CONTROL_REQUEST_HEADERS ']}”);退出(0);}