在设置标题以发送请求到spring方面,我遇到了问题。下面是我的代码来设置标题,但我仍然得到这个错误。
OPTIONS http://localhost/orgchart/app/json/depts 403 (Forbidden) angular.js:7978
OPTIONS http://localhost/orgchart/app/json/depts No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. angular.js:7978
XMLHttpRequest cannot load http://localhost/orgchart/app/json/depts. No 'Access-Control- Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. 我的角度码在下面,有什么想法吗?
orgChartApp.factory('Depts', function($http) {
$http.defaults.headers.put = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With'
};
var departmentService = {
putDepartment: function(dept) {
var promise = $http.put('http://localhost/orgchart/app/json/depts', {
name: dept.name,
parentDepartmentId: dept.parentDepartment.id,
}).then(function(response) {
return response.data;
});
return promise;
}
};
return departmentService;
});发布于 2014-02-10 21:41:41
我修复了这个问题,首先,我的服务器端没有返回一个ResponseBody,所以如果我期望任何返回值,那么这就是一个大问题。
其次,我发送单独的对角,并确保为我的后端添加一个头,如下所示。
putDepartment: function(dept) {
var promise = $http({
url: 'http://localhost/orgchart/app/json/dept/' + dept.id,
method: 'PUT',
params: {
name: dept.name,
parentDepartmentId: dept.parentDepartment.id
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).then(function(response) {
return response.data.depts;
});
return promise;
}https://stackoverflow.com/questions/21682195
复制相似问题