请求的资源角度 js 上不存在“访问控制允许源”标头
No 'Access-Control-Allow-Origin' header is present on the requested resource- angular js
我正在使用php codeigniter restful api作为服务器端和客户端应用程序的离子框架开发应用程序,我正在尝试将客户端应用程序与服务器 api 连接,但遇到以下问题
XMLHttpRequest cannot load http://localhost:90/mrk/index.php/MRKGeneral_api/user_login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
我在客户端使用了下面的代码,
$scope.signIn = function(form) {
if(form.$valid) {
var postData = { 'username' : $scope.authorization.username, 'password' : $scope.authorization.password };
$http({
url: "http://localhost:90/mrk/index.php/MRKGeneral_api/user_login", method: 'POST',
data: postData,
headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
}).then(function(result) {
if(result.status == 200) {
if(result.data.success) {
GeneralService.userType = result.data.usertype;
GeneralService.userData = result.data;
$scope.errormsg = result.data.message;
$state.go('/app/dashboard');
} else {
$scope.errormsg = result.data.message;
}
} else {
}
});
}
}
在我的服务器端,我也设置了标题,如下所示,
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER['REQUEST_METHOD'];
if($method == "OPTIONS") {
die();
}
但仍然得到 请求的资源上不存在"访问控制允许源"标头。因此,不允许访问源"http://localhost:8100"。
您需要
在应用配置中允许跨域调用
app.config(function($httpProvider) {
//Enable cross domain calls
$httpProvider.defaults.useXDomain = true;
});
相关文章:
- Javascript 受客户端和访问控制允许源的限制
- Azure Blob 存储 请求的资源错误中不存在访问控制允许源标头
- Socket.io 请求的资源上不存在“访问控制允许源”标头.因此,不允许访问源“http://localhost”
- Android 4.1 上的访问控制允许源错误
- 跨源资源错误:请求的资源上不存在“访问控制允许源”标头
- 访问控制允许源错误
- 意外的访问控制允许源错误
- CORS 标头“访问控制允许源”丢失,但它存在于标头中
- 访问控制允许源不允许 AJAX 源空
- 交叉请求时出错:“访问控制允许源不允许源”
- 将所有域添加到 CORS(访问控制允许源:*)的安全隐患
- Foursquare 返回“无访问控制允许源”标头显示错误
- 是来自浏览器或 XmlHttpRequest 的“没有'访问控制-允许源'标头”错误消息
- 旧系统的 CORS 问题 - 没有“访问控制允许源”标头
- AJAX 请求收到“请求的资源上不存在'访问控制-允许源'标头”错误
- 执行预检请求时是否需要访问控制允许源 CORS 标头
- ajax jonp 不应该适用于 请求的上不存在“访问控制-允许源”标头
- 为什么如果此 API 给出“无访问控制允许源标头”,我可以通过地址栏或 http://www.hurl.it 访问它
- 在某些情况下没有“访问控制-允许源”标头错误,但在其他情况下则没有
- 无法在使用 CORS 和 Mandrill 的 Node Express 中找出“没有'访问控制-允许源'