Tôi đã tìm kiếm trên 200 trang web (có thể phóng đại, nhưng không nhiều) về cách có thể xử lý cors với angularjs. Chúng tôi có một máy địa phương đang chạy một máy chủ API web. Chúng tôi đang phát triển một ứng dụng khách gọi API cho dữ liệu. Khi chạy máy khách từ máy chủ, chúng tôi nhận được dữ liệu không có vấn đề gì. Khi chúng tôi chạy nó từ một miền khác, chúng tôi nhận được phản hồi 200 màu đỏ khi cố gắng tìm nạp dữ liệu và phản hồi trống. Dưới đây là một số mã:AngularJS CORS Các vấn đề
var myApp = angular.module('Project', ['ngResource']);
myApp.config(function($routeProvider){
$routeProvider.
when('/new', {templateUrl:'templates/new.html', controller:'EditProjectController'}).
when('/mobile', {templateUrl:'templates/mobile.html', controller:'ProjectController'}).
when('/it', {templateUrl:'templates/it.html', controller:'ProjectController'}).
when('/writing', {templateUrl:'templates/writing.html', controller:'ProjectController'}).
when('/all', { templateUrl: 'templates/all.html' }).
when('/login', { templateUrl: 'partials/_login.html' }).
otherwise({ redirectTo: '/all' });
});
myApp.config(['$httpProvider', function ($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
]);
myApp.controller('ProjectController',
function myApp($scope, $http, projectDataService, userLoginService) {
$http.defaults.useXDomain = true;
$scope.loadProject = function(){
projectDataService.getProject(function(project){
$scope.project = project;
})
};
$scope.loadProject();
}
);
myApp.factory('projectDataService', function ($resource, $q) {
var resource = $resource('http://webapiserver/api/:id', { id: '@id' });
return {
getProject: function() {
var deferred = $q.defer();
resource.query({ id: 'project' },
function (project) {
deferred.resolve(project);
},
function (response) {
deferred.reject(response);
});
return deferred.promise;
},
save: function (project) {
var deferred = $q.defer();
project.id = 'project/9';
resource.save(project,
function (response) { deferred.resolve(response); },
function (response) { deferred.reject(response); }
);
return deferred.promise;
}
};
});
Tôi cũng đã cố gắng này sử dụng $ http nhưng tôi nhận được câu trả lời tương tự (hoặc thiếu):
myApp.factory("projectDataService", function ($http) {
return {
getProject: function (successcb) {
$http.get("http://webapiserver/api/project").
success(function (data, status, headers, config) {
successcb(data);
}).
error(function (data, status, headers, config) {
}
};
});
Khi tôi chỉ cần duyệt đến url được phục vụ tối đa các json trong trình duyệt nó spits ra các dữ liệu. Trên máy chủ, chúng tôi cho phép nguồn gốc tên miền chéo rõ ràng bằng tuyên bố trước đó của tôi. Như bạn có thể thấy tôi đang thực hiện các tiêu đề ghi đè trong myApp.config Tôi thậm chí đã thử đặt nó trực tiếp trong bộ điều khiển của tôi ... không có sự khác biệt ...
3 ngày bây giờ về nhiệm vụ này.
Trợ giúp với điều này là THÊM được đánh giá cao. Cảm ơn trước.
Vì vậy, yêu cầu GET của bạn không thành công?Vui lòng hiển thị tiêu đề của yêu cầu và phản hồi. Sẽ dễ nhất nếu bạn chỉ cần mở tab mạng trên Chrome và bao gồm mọi thứ trong phần tiêu đề. –
Bạn có thể thấy tiêu đề tên miền chéo và bắt tay Tùy chọn xảy ra nếu bạn xem xét XHRRequest trong devtools trong Chrome chẳng hạn –