$http Auth Headers in AngularJS

前端 未结 8 730
闹比i
闹比i 2020-11-30 21:52

I have an angular application that is hitting a node API. Our backend developer has implemented basic auth on the API, and I need to send an auth header in my request.

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 22:28

    In the angularjs documentation you can see some ways to set headers but I think this is what you are searching:

    $http({
        method: 'POST',
        url: '/theUrl',
        headers: {
            'Authorization': 'Bearer ' + 'token'
             //or
             //'Authorization': 'Basic ' + 'token'
        },
        data: someData
    }).then(function successCallback(response) {
        $log.log("OK")
    }, function errorCallback(response) {
        if(response.status = 401){ // If you have set 401
            $log.log("ohohoh")
        }
    });
    

    I'm using this structure in my angularjs client with an ASP.NET 5 server and it works.

提交回复
热议问题