Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use HTTPS in AngularJS?

I am using AngularJS, $resource & $http and working with apis, however due to security reason I need to make an HTTPS request (work under the HTTPS protocol).

What's the way to use https in AngularJS.

Thanks for your help.

like image 653
Dick Van Ocampo Avatar asked Sep 17 '13 00:09

Dick Van Ocampo


People also ask

What is http service in angular?

The $http service is a core AngularJS service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP. For unit testing applications that use $http service, see $httpBackend mock.

Is HTTP request is synchronous or asynchronous in AngularJS?

The problem is as follows, $http. get is asynchronous, before the response is fetched, the function returns. Therefore the calling function gets the data as empty string.

What is http get in AngularJS?

get Method. In angularjs $http service is used to send or get data from remote http servers using browsers XMLHttpRequest object. In angularjs $http service is having many shortcut methods available like $http.

Which controls are provided by AngularJS to read data from server?

AngularJS provides $https: control which works as a service to read data from the server. Server can make a database call to get the records. AngularJS needs data in JSON format. Once data is ready, $https: can be used to get the data from server in the following manner.


2 Answers

For some reason Angular would send all requests over HTTP if you don't have a tailing / at the end of your requests. Even if the page itself is served through HTTPS.

For example:

$http.get('/someUrl').success(successCallback);  // Request would go over HTTP even if the page is served via HTTPS 

But if you add a leading / everything would work as expected:

$http.get('/someUrl/').success(successCallback);  // This would be sent over HTTPS if the page is served via HTTPS 

EDIT: The root cause of this problem is that Angular looks at the actual headers from the server. If you have an incorrect internal pass of http data through https there will still be http headers and Angular would use them if you do not add / at the end. i.e. If you have an NGINX serving content through https, but passing requests to Gunicorn on the backend via http, you might have this issue. The way to fix that is to pass the correct headers to Gunicorn, so your framework would be under the impression of being served via https. In NGINX you can do this with the following line:

proxy_set_header X-Forwarded-Proto $scheme;

like image 192
Danil Avatar answered Nov 08 '22 12:11

Danil


Use the $http api as you would normally:

$http.get('/someUrl').success(successCallback); 

if your app is being served over HTTPS then any calls you are making are to the same host/port etc so also via HTTPS.

If you use the full URIs for your requests e.g. $http.get('http://foobar.com/somePath') then you will have to change your URIs to use https

like image 43
craigb Avatar answered Nov 08 '22 12:11

craigb