Angularjs Apache2 Symfony2 Cors
Angularjs Apache2 Symfony2 Cors
我正在尝试构建一个 RESTFUL API。应用程序的结构应如下所示:
我有 2 个域 "ui.domain.com"、"api.domain.com"
"ui" 域服务前端应用程序,使用 AngularJS 在 JavaScript 中编写。
"api" 域作为后端,使用 Symfony2
在 PHP 中编写
出现的问题是当我尝试从前端发送请求时
//I have set
//$http.defaults.headers.common['Authorization'] = 'Basic ' + Session.apiKey;
//in angularjs run configuation
//in one of my controllers I have test function
$scope.test = function () {
$http.get('http://api.domain.com/api/v1/test');
};
但是当我发送 GET 请求时,它会转换为 "OPTIONS",我会收到 405 Http 错误。
//Request, Response log from Chrome
General:
Remote Address:**.**.**.**:80
Request URL:http://api.domain.com/api/v1/test
Request Method:OPTIONS
Status Code:405 Method Not Allowed
Response Headers:
Access-Control-Allow-Headers:Authorization, X-Requested-With, Content-Type, Accept, Origin
Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT, PATCH, DELETE
Access-Control-Allow-Origin:http://ui.domain.com
Cache-Control:no-cache
Connection:Keep-Alive
Content-Length:123
Content-Type:application/json
Date:Fri, 24 Apr 2015 06:58:34 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.7 (Ubuntu)
X-Powered-By:PHP/5.5.9-1ubuntu4.7
Request Headers:
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:lt,en-US;q=0.8,en;q=0.6
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:GET
Connection:keep-alive
Host:api.domain.com
Origin:http://api.domain.com
Referer:http://ui.domain.com/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
以下是我设置 "api" sub-domain VH
的方法
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "http://mdm-ui.sepikas.eu"
Header set Access-Control-Allow-Methods "POST, GET, OPTIONS, PUT, PATCH, DELETE"
Header set Access-Control-Allow-Headers "Authorization, X-Requested-With, Content-Type, Accept, Origin"
</IfModule>
在我的 Symfony2 后端我有路线:
/**
* @Get("/test")
*/
public function testAction()
{
return new JsonResponse(array(
'message' => 'ok'
));
}
奇怪的是,即使我明确添加了授权 header,我也没有在 Chrome 开发人员工具提供的请求 header 列表中看到它。
根据我在其他帖子和一些博客上的阅读,这不是 AngularJS 部分的问题。但是为什么即使我在我的服务器配置中允许 CORS,我仍然会收到 405 错误。我怀疑这可能是因为我在我的路线中只允许 get 方法:
/**
* @Get("/test")
*/
所以我尝试将其更改为
/**
* @Options("/test")
*/
即使这没有意义,因为我不想只允许 "GET" 方法,即使它随后返回 401 错误而不是 405。
我很困惑,不确定接下来要尝试什么,我不想做的事情在我的选择中非常简单:允许 CORS 到我的 "api" 后端并发送常规 "GET"、"POST" 等等...
请求我的后端而不以某种特定方式更改它,例如添加额外的 "OPTIONS" 方法来路由。
我在 API 中使用 Nelmio CORS Bundle,我从来没有遇到过 CORS 问题。 https://github.com/nelmio/NelmioCorsBundle
我正在尝试构建一个 RESTFUL API。应用程序的结构应如下所示: 我有 2 个域 "ui.domain.com"、"api.domain.com"
"ui" 域服务前端应用程序,使用 AngularJS 在 JavaScript 中编写。 "api" 域作为后端,使用 Symfony2
在 PHP 中编写出现的问题是当我尝试从前端发送请求时
//I have set
//$http.defaults.headers.common['Authorization'] = 'Basic ' + Session.apiKey;
//in angularjs run configuation
//in one of my controllers I have test function
$scope.test = function () {
$http.get('http://api.domain.com/api/v1/test');
};
但是当我发送 GET 请求时,它会转换为 "OPTIONS",我会收到 405 Http 错误。
//Request, Response log from Chrome
General:
Remote Address:**.**.**.**:80
Request URL:http://api.domain.com/api/v1/test
Request Method:OPTIONS
Status Code:405 Method Not Allowed
Response Headers:
Access-Control-Allow-Headers:Authorization, X-Requested-With, Content-Type, Accept, Origin
Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT, PATCH, DELETE
Access-Control-Allow-Origin:http://ui.domain.com
Cache-Control:no-cache
Connection:Keep-Alive
Content-Length:123
Content-Type:application/json
Date:Fri, 24 Apr 2015 06:58:34 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.7 (Ubuntu)
X-Powered-By:PHP/5.5.9-1ubuntu4.7
Request Headers:
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:lt,en-US;q=0.8,en;q=0.6
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:GET
Connection:keep-alive
Host:api.domain.com
Origin:http://api.domain.com
Referer:http://ui.domain.com/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
以下是我设置 "api" sub-domain VH
的方法<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "http://mdm-ui.sepikas.eu"
Header set Access-Control-Allow-Methods "POST, GET, OPTIONS, PUT, PATCH, DELETE"
Header set Access-Control-Allow-Headers "Authorization, X-Requested-With, Content-Type, Accept, Origin"
</IfModule>
在我的 Symfony2 后端我有路线:
/**
* @Get("/test")
*/
public function testAction()
{
return new JsonResponse(array(
'message' => 'ok'
));
}
奇怪的是,即使我明确添加了授权 header,我也没有在 Chrome 开发人员工具提供的请求 header 列表中看到它。
根据我在其他帖子和一些博客上的阅读,这不是 AngularJS 部分的问题。但是为什么即使我在我的服务器配置中允许 CORS,我仍然会收到 405 错误。我怀疑这可能是因为我在我的路线中只允许 get 方法:
/**
* @Get("/test")
*/
所以我尝试将其更改为
/**
* @Options("/test")
*/
即使这没有意义,因为我不想只允许 "GET" 方法,即使它随后返回 401 错误而不是 405。
我很困惑,不确定接下来要尝试什么,我不想做的事情在我的选择中非常简单:允许 CORS 到我的 "api" 后端并发送常规 "GET"、"POST" 等等... 请求我的后端而不以某种特定方式更改它,例如添加额外的 "OPTIONS" 方法来路由。
我在 API 中使用 Nelmio CORS Bundle,我从来没有遇到过 CORS 问题。 https://github.com/nelmio/NelmioCorsBundle