javascript - 如何在 AngularJS 1.6 上正确添加 headers

标签 javascript angularjs http-headers httprequest

我正在尝试使用 $http 在获取请求中添加 header 。

这是我的代码:

this.http.defaults.headers.common['Authorization'] = 'Bearer mytoken123123';
this.http.defaults.headers.common['Access-Control-Allow-Origin'] =  '*';
this.http.defaults.headers.common['Access-Control-Allow-Methods'] ='GET, POST, PATCH, PUT, DELETE, OPTIONS';
this.http.defaults.headers.common['Access-Control-Allow-Headers'] = 'Origin, Content-Type, X-Auth-Token';
this.http.defaults.headers.common['Access-Control-Allow-Credentials'] = 'true';

this.http.get(" https://xxxxxx/GetUserProfille?User_Id=" + response.data.Data.User_Id + "&LanguageCode=" + this.currentLang).then((response) => {
     console.log(response)
});

这就是错误。

angular.min.js:107 OPTIONS 
https://xxxxxxx/GetUserProfille?User_Id=11641&LanguageCode=it 405 (Method Not Allowed)
https://xxxxxxx/GetUserProfille?User_Id=xxx&LanguageCode=xx: Response to 
preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:8080' is therefore not allowed access. The response had HTTP status code 405.

angular.min.js:122 Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":" https://staging.paladintrue.com/api/paladin/GetUserProfille?User_Id=11641&LanguageCode=it","headers":{"Accept":"application/json, text/plain, */*","Authorization":"bearer 2kqV-V3v1t_6MtI8Vg8BPpoq2BXR6Q1vqEu9rKw6Qd86CHqKxtYIQfjJhUJqM5Jzi2K0h6sAXaltIxCOoEXUCz8UeHFs7HGhxY2t22yBPUuRRGzmu7O52y_AjdHwlijsEW6KDzqWzhR5hSqNrYm51yHCNmfhkkhocOt8_EDwNljnKycIdYXOOApzaSF_Jm88eJ9cMilTavsmgqd_DAAuSV24aNqf6cJdZYE0w_KTHk7P--oul4SEgpfQYW64PNSU","Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET, POST, PATCH, PUT, DELETE, OPTIONS","Access-Control-Allow-Headers":"Origin, Content-Type, X-Auth-Token","Access-Control-Allow-Credentials":"true"}},"statusText":""}

如果你在浏览器中看到 headers 请求,看来我添加的 headers 都是在 Access-Control-Request-Headers:access-control-allow-credentials,access-control-allow 中- header ,访问控制允许方法,访问控制允许来源,授权 enter image description here

所以,我知道这不是添加 header 的正确方法。

最佳答案

您可以将 HTTP header 作为 $http configuration object 的一部分传递:

$http.get('https://www.example.com/', {
    headers: {
        'Authorization': 'Bearer mytoken123123',
        'Access-Control-Allow-Origin': '*'
        // etc.
    }
});

关于javascript - 如何在 AngularJS 1.6 上正确添加 headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48391109/

相关文章:

javascript - JS string.split() 不删除分隔符

javascript - php mysql 后台进程

javascript - Angular.js - ng-model 在文本输入上覆盖我的 ng-value

nginx - 在 Nginx 中获取 Http Referer 的主机名

javascript - 用 javascript 调用替换内部 html 时出错

javascript - 关于 webkit-min-device-pixel-ratio 的问题

IIS 7.5 从响应中删除 etag header

c getaddrinfo 没有这样的主机是已知的

javascript - 仅当用户在搜索字段中键入时使数据列表可见

javascript - 单击时从自定义指令调用函数? - Angular JS