angularjs – Restangular跨域请求.我做错了什么?
发布时间:2020-12-17 10:20:13 所属栏目:安全 来源:网络整理
导读:我的域sub.example.com配置了restangular: RestangularProvider.setDefaultHeaders({ 'Content-Type': 'application/json','X-Requested-With': 'XMLHttpRequest'});RestangularProvider.setDefaultHttpFields({ 'withCredentials': true}); 然后我通过以下
我的域sub.example.com配置了restangular:
RestangularProvider.setDefaultHeaders({ 'Content-Type': 'application/json','X-Requested-With': 'XMLHttpRequest' }); RestangularProvider.setDefaultHttpFields({ 'withCredentials': true }); 然后我通过以下方式建造其他工厂: return Restangular.withConfig(function(RestangularProvider) { RestangularProvider.setBaseUrl('http://api.example.com'); }); 并且,显然,获取错误否请求资源上存在“Access-Control-Allow-Origin”标头.因此,不允许来源“http://sub.example.com”访问..如何配置服务器/客户端以获取有效的跨域请求? //更新 我在后端使用Yii并发送下一个标题
我找到了解决方案.
首先,当使用凭证时 – 我们不能使用*来访问Access-Control-Allow-Origin. // scheme required,here can be multiple origins concatenated by space if using credentials header('Access-Control-Allow-Origin: http://sub.example.com'); header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE'); header('Access-Control-Allow-Headers: Accept,X-Requested-With'); // without credentials we can use * for origin header('Access-Control-Allow-Credentials: true'); header('HTTP/1.1 200 OK',true); 然后我们可以简单地使用crossdomain ajax请求. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读