gpt4 book ai didi

javascript - $http 设置 header 错误 - angularjs

转载 作者:可可西里 更新时间:2023-11-01 16:43:11 24 4
gpt4 key购买 nike

我在设置 header 以将请求发送到 spring mvc 时遇到问题。下面是我设置标题的代码,但我仍然收到此错误。

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.

我的 Angular 代码如下,有什么想法吗?

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;
});

最佳答案

我解决了这个问题,首先我的服务器端点没有返回 ResponseBody,所以如果我期待任何返回值,那将是一个大问题。

其次,我发送了单独的参数,并确保像这样为我的后端添加一个 header 。

    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;
}

关于javascript - $http 设置 header 错误 - angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21682195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com