AngularJs http请求标头不会更改

时间:2015-10-15 14:47:51

标签: angularjs http authentication http-headers http-post

当我尝试调用我的WebApi时遇到问题,我需要在请求标头中发送授权凭据,但是,我没有将其启动。

我的resquest标头需要像我的REST测试器生成的请求标头的图像

Authorization:Basic dXNlcm5hbWU6cGFzc3dvcmQ=

但是,当我尝试使用此代码块在Angular上设置它时

$http.defaults.headers.Authorization = 'Basic ' + credentials;
return $http.post('http://localhost:2703/api/Authenticate');

或者这个

return $http.post('http://localhost:2703/api/Authenticate', {
    headers: { 'Authorization': 'Basic ' + credentials }
});

我的请求标题变得像这样

Access-Control-Request-Headers: accept, authorization

有人能告诉我我做错了什么吗?任何帮助将不胜感激。 :)

2 个答案:

答案 0 :(得分:1)

您可以这样做:

var header = {headers: {'Authorization': 'Basic ' + credentials}}
$http.post(url, payload, header)
    .success(function (data) {
       //stuff
    }

答案 1 :(得分:0)

我无法使用跨域请求解决我的问题,因此,我在同一个域中配置了我的Web Api和Web App。这样做,我可以正常更改标题并根据需要发送帖子请求。