AngularJS-阻止跨源请求
我正在开发一个AngularJS项目,并且正在使用一个外部Symfony REST api。但是,Angular/JS阻止了我的请求。我做了一些研究,并在我的应用程序的配置部分添加了以下行:AngularJS-阻止跨源请求,angularjs,request,Angularjs,Request,我正在开发一个AngularJS项目,并且正在使用一个外部Symfony REST api。但是,Angular/JS阻止了我的请求。我做了一些研究,并在我的应用程序的配置部分添加了以下行: angular .module('appName', [ .... ]) .config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) { $httpProvider.defaults.us
angular
.module('appName', [
....
])
.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
..
但是,firebug仍然抛出一个错误:跨源请求被阻止。。
当我查看firebug的“网络”选项卡时,我得到的是:
对这个问题有什么想法吗?
提前感谢。看起来您的symfony2应用程序在选项请求响应中返回了不适当的CORS头 在一些项目中,我将symfony2与angularjs结合使用,在angularjs方面并没有什么特别之处可以让它工作 返回我在symfony2应用程序中使用的正确CORS头 有来自dev服务器的配置:
nelmio_cors:
defaults:
allow_credentials: false
allow_origin: []
allow_headers: []
allow_methods: []
expose_headers: []
max_age: 0
hosts: []
paths:
'^/api/':
allow_origin: ['*']
allow_headers: ['X-Requested-With','Content-Type','Authorization']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
allow_credentials: true
max_age: 3600
看起来您的symfony2应用程序在选项请求响应中返回了不适当的CORS头 在一些项目中,我将symfony2与angularjs结合使用,在angularjs方面并没有什么特别之处可以让它工作 返回我在symfony2应用程序中使用的正确CORS头 有来自dev服务器的配置:
nelmio_cors:
defaults:
allow_credentials: false
allow_origin: []
allow_headers: []
allow_methods: []
expose_headers: []
max_age: 0
hosts: []
paths:
'^/api/':
allow_origin: ['*']
allow_headers: ['X-Requested-With','Content-Type','Authorization']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
allow_credentials: true
max_age: 3600
这对我不起作用。在尝试了所有方法之后,我在本地主机上创建了一个PHP代理页面,它执行以下操作
<?php echo file_get_contents($_REQUEST['url']); ?>
只是别忘了对URL编码,它对我不起作用。在尝试了所有方法之后,我在本地主机上创建了一个PHP代理页面,它执行以下操作
<?php echo file_get_contents($_REQUEST['url']); ?>
别忘了给URL编码你的答案让我很高兴!你的回答让我大吃一惊!