Basic authentication not working in ionic

I am using a basic authentication in my ionic app. I am using $http.jsonp & $http.post in my services, my jsonp services are fetching data if I am attaching username and password with the url like - http://usernameString:PasswordString@localhost:3000

BUT, $http.post service is throwing the following error -

Error: Access to restricted URI denied

Though its working in Chrome but not in firefox(following error is in Firefox Version 33).

Let me know how can I fix the error for Firefox.

you can try setting the header?

        var authdata = Base64.encode(username + ':' + password);
        $http.defaults.headers.common['Authorization'] = 'Basic ' + authdata; 

you will need to do the base64 encoding, there are a few options you can google… http://stackoverflow.com/questions/246801/how-can-you-encode-a-string-to-base64-in-javascript
https://github.com/ninjatronic/angular-base64

Thanks for answering, but I supplied that headers already, still not working and following is my service method code -

serviceUserMethod: function (userId, questionnaireId,data) { if(typeof analytics !== 'undefined') { analytics.trackEvent('My App', '/userapis/my_user_data'); } $window.ga('send', 'event', 'my App', '/userapis/my_user_data'); var url = 'http://localhost:3000'; $http.defaults.headers.common['Authorization'] = 'Basic ' + '9814abeb5f0f21087824fe71:0101d05c-119111e6'; //return $http.post(url + '/userapis/my_user_data/' + userId + '/'+questionnaireId, {headers: {'Authorization': 'Basic 9814abeb5f0f21087824fe71:0101d05c-119111e6'}},data); return $http.post(url + '/userapis/my_user_data/' + userId + '/'+questionnaireId, data, {headers: {'Authorization': 'Basic 9814abeb5f0f21087824fe71:0101d05c-119111e6'}}); },

BUT even I am getting CORS error in my firebug, though I set proper headers at my server end.

Please check the req, res headers I am receiving -

your headers are incorrect because they are not base64 encoded… take a look at the code snippet i provided again… it works in postman because it will encode the credentials for you

Ok but why did i get a OPTIONS request when doing a $http.post ?

I used the header you specified, in postman and in web browser, it works. But on android, it shows up the standard http basic auth dialog, “authentication required” with username and password fields…no matter what is entered, it keeps prompting the dialog. And on iOS, nothing shows up and it doesnt work.

Why is that?

1 Like

Chrome is preflighting the request to look for CORS headers. See http://stackoverflow.com/a/21783145/878402 for more information.

VolvoOlympian I also get this issue :frowning: