在AngularJS中,我在子域中使用了Restful API但是我遇到的问题是cookie /会话没有跨域共享.对于Angular,我这样做:
app.config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.useXDomain = true; $httpProvider.defaults.withCredentials = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; }]);
此外,当我用$ http发出请求时,我正在做
var object = {}; object.url = '/example' object.withCredentials = true; $http(object).success(object.success).error(object.error);
在我的服务器端,我有:
if($_SERVER['REQUEST_METHOD']=='OPTIONS') { if(isset($_SERVER['HTTP_X_FOWARDED_HOST']) && !empty($_SERVER['HTTP_X_FOWARDED_HOST'])) { $origin=$_SERVER['HTTP_X_FOWARDED_HOST']; } else { $origin=$_SERVER['HTTP_ORIGIN']; } if(isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && ($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']=='POST' || $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']=='DELETE' || $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']=='PUT')) { header('Access-Control-Allow-Origin: '.$origin); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Allow-Headers: *,X-Requested-With,Content-Type'); //header('Access-Control-Allow-Headers: Content-Type'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT'); // http://stackoverflow.com/a/7605119/578667 header('Access-Control-Max-Age: 86400'); } }
现在我看到服务器说它将允许凭据但不在选项请求中发送.截图如下.
我究竟做错了什么?