Javascript AngularJS$resource中的基本HTTP身份验证
我正在尝试构建一个简单的AngularJS应用程序,它通过$resource使用api。一切都很顺利,直到我在服务中添加了基本的HTTP身份验证。现在,当我尝试添加http身份验证头时,我得到一个错误:Javascript AngularJS$resource中的基本HTTP身份验证,javascript,angularjs,rest,http,authentication,Javascript,Angularjs,Rest,Http,Authentication,我正在尝试构建一个简单的AngularJS应用程序,它通过$resource使用api。一切都很顺利,直到我在服务中添加了基本的HTTP身份验证。现在,当我尝试添加http身份验证头时,我得到一个错误: "XMLHttpRequest cannot load http://localhost:8000/api/v1/course. The request was redirected to 'http://localhost:8000/api/v1/course/', which is d
"XMLHttpRequest cannot load http://localhost:8000/api/v1/course. The request was redirected to 'http://localhost:8000/api/v1/course/', which is disallowed for cross-origin requests that require preflight."
但是,当我试图通过$http从同一api获取数据时,身份验证工作正常。
问题在哪里
这是我的密码:
service.js:
/*cwApp.factory('courseFactory', function($http){
return {
getAllCourses: function(data, status, headers, config){
$http.get('http://localhost:8000/api/v1/course/')
.success(data, status, headers, config)
.error(data, status, headers, config);
}
};
});*/
angular.module('courseModule', ['ngResource'])
.factory('courseResource', function($resource){
return $resource('http://localhost\\:8000/api/v1/course/:id');
});
app.js:
var cwApp = angular.module('cwApp', ['courseModule', 'ngRoute']);
cwApp.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
]);
cwApp.config(function ($routeProvider){
$routeProvider
.when('/', {
templateUrl: 'app/partials/start.html'
})
.when('/allcourses', {
controller: 'AllCoursesController',
templateUrl: 'app/partials/all_courses_view.html'
})
.otherwise({
redirectTo: '/'
});
});
controller.js:
cwApp.controller('AllCoursesController', function ($scope, $http, courseResource){
$http.defaults.headers.common['Authorization'] = 'Basic ' + window.btoa("dmitry" + ":" + "starkapp");
/*courseFactory.getAllCourses(function(data, status, headers, config){
$scope.courses = data["objects"];
console.log('HTTP Status: ' + status);
});*/
courseResource.get(function(data){
$scope.courses = data["objects"];
});
});