angularjs - $http.post not working with header -


i new angularjs. trying make api request requires authorization. have included in header of request, still not working. looking in chrome network tab, shows method used options , not post.

app.config(['$httpprovider', function ($httpprovider) {     $httpprovider.defaults.headers.common = {};     $httpprovider.defaults.headers.put = {};     $httpprovider.defaults.headers.patch = {};     $httpprovider.defaults.headers.post = {};     $httpprovider.defaults.headers.post["content-type"] = "application/x-www-form-urlencoded"; }]);  app.service('api', function ($http) {     return {         post: function(token){             $http.defaults.headers.post['authorization'] = "bearer " + token             $http.post('http://localhost:3000/api/profile/me').success(function(data, status, headers, config) {                 console.log(data)             })         }       } }); 

i'm guessing "it's not working" mean you're getting 401. issue iis , cors. whenever have cors enabled on api requests send preflighted options request. problem options request doesn't have authentication headers , iis wants authentication headers, doesn't find them responds 401. ran same issue , found fix in this article. if you're using integrated mode api on iis fix easy. important stuff link below.

create ihttpmodule:

public class corsmodule : ihttpmodule {     public void dispose() { }      public void init(httpapplication context)     {         context.presendrequestheaders += delegate         {             if (context.request.httpmethod == "options")             {                 var response = context.response;                 response.statuscode = (int)httpstatuscode.ok;             }         };     } } 

and register in config

<system.webserver>   <httpprotocol>     <customheaders>       <add name="access-control-allow-methods" value="post,get,options" />       <add name="access-control-allow-origin" value="*" />       <add name="access-control-allow-headers" value="content-type,authorization" />     </customheaders>   </httpprotocol>   <modules>     <add name="corsmodule" type="corsmodule" />   </modules> </system.webserver> 

what creates ihttpmodule runs before iis's authorization (only if you're in integrated mode, if you're in classic mode haven't found fix this), checks see if it's options request , allows it.

hope helps.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -