【问题标题】:How to add header in to $http.post [duplicate]如何将标头添加到 $http.post [重复]
【发布时间】:2017-09-12 14:46:07
【问题描述】:

我有以下用于引导输入的代码。

return $http.post(searchClients, {
      params: {
            clientName: val,
     }
}).then(function(response){
    return response.data.results;
   });

我需要在标题中添加一个标记。 如何将令牌添加到标头?

searchClients 保存在别处定义的 URL。 此代码有效,我只需要知道如何在标题中添加一些额外信息。

【问题讨论】:

  • 你试过了吗:标题:{'Content-type': 'application/json', 'Authorization': 'Bearer' + window.localStorage.getItem('token') },
  • 尝试连接告诉您HTTP headers 的服务并捕获(仅打印纯 HTML)输出。至少你会知道你的标题是否真的在途中丢失了,或者可能是别的什么

标签: angularjs twitter-bootstrap angular-ui-bootstrap typeahead


【解决方案1】:
    $http({
    method: 'POST',
     headers: {
        'Content-Type': 'application/json;charset=UTF-8',
         'X-TOKEN': '9AF4E2526279434180C7365EF86F6E77'
    },
    url: 'http://xxx/User/Validate?UserName='+username+'&Password='+password+,

    data: {username:username, password:password}
}).then(function successCallback(response) {
    console.log(response);
     var logininfo = (response);
     localStorage.setItem("user_info", JSON.stringify(logininfo));
     $state.go('home');
}, function errorCallback(response) {
     console.log(response)
    if(response.status = 401){ //
       $scope.showpopup1("Message","invaild Username && Password");
    }
});

【讨论】:

  • 你可以跳过.then之后的所有代码
猜你喜欢
  • 2017-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 2020-04-27
  • 2020-08-14
相关资源
最近更新 更多