首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$http设置标头bug - angularjs

$http设置标头bug - angularjs
EN

Stack Overflow用户
提问于 2014-02-10 16:11:01
回答 1查看 2.2K关注 0票数 2

在设置标题以发送请求到spring方面,我遇到了问题。下面是我的代码来设置标题,但我仍然得到这个错误。

代码语言:javascript
复制
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. 

我的角度码在下面,有什么想法吗?

代码语言:javascript
复制
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;
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-10 21:41:41

我修复了这个问题,首先,我的服务器端没有返回一个ResponseBody,所以如果我期望任何返回值,那么这就是一个大问题。

其次,我发送单独的对角,并确保为我的后端添加一个头,如下所示。

代码语言:javascript
复制
    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;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21682195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档