Questions tagged [angular-http]

AngularJS tag for $http service is a core Angular service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP.

The $http service is a core Angular 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.

For a higher level of abstraction, please check out the $resource service.

The $http API is based on the deferred/promise APIs exposed by the $q service. While for simple usage patterns this doesn't matter much, for advanced usage it is important to familiarize yourself with these APIs and the guarantees they provide.

General usage

The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise with two $http specific methods: .then and .catch.

// Simple GET request example :
$http.get('/someUrl')
  .then(function(response) {
    var data = response.data;
    // this callback will be called asynchronously
    // when the response is available
}).catch(function(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});
// Simple POST request example (passing data) :
$http.post('/someUrl', {msg:'hello word!'})
  .then(function(response) {
    // this callback will be called asynchronously
    // when the response is available
}).catch(function(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

Since the returned value of calling the $http function is a promise, you can also use the then method to register callbacks, and these callbacks will receive a single argument – an object representing the response. See the API signature and type info below for more details.

A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the error callback will not be called for such responses.

970 questions
6
votes
1 answer

AngularJS: Setting correct 'Referer' header for $http

It looks like when you send off a request using the $http service, the referer header is set to the base url without the angular $location information. How do you go about appending the $location.path() to that header for all requests? Our API calls…
John
  • 17,163
  • 16
  • 65
  • 83
6
votes
1 answer

Non blocking Ajax request to PHP

I'm using PHP to download a (big) file from a remote server and this download is triggered by clicking on a download button on a web page. So when i click the download button on the web page, then an Ajax request (with angulars $http) is made to a…
Vivendi
  • 20,047
  • 25
  • 121
  • 196
6
votes
1 answer

Paypal with angularjs $http.post request

This is the standard paypal form for buying stuff.
Jaanus
  • 16,161
  • 49
  • 147
  • 202
5
votes
0 answers

How to use finally in HttpClient Angular 7

In the previous versions of Angular, I use finally() just fine like the code below constructor( private http: Http ) { } const headers = new Headers({ 'Authorization': this.authenticate.getToken(), 'Content-Type': 'application/json' }); const…
Char
  • 2,073
  • 8
  • 28
  • 45
5
votes
1 answer

Angular 5 http.get into a BehaviorSubject to multicast data changes on the client

I have an angular 5 website which consumes a restful wcf web service. The web service, in turn, interacts with business objects, that talk to a database. I'm using the @angular/common/http module to consume the web service into an observable…
Bill Mild
  • 413
  • 1
  • 4
  • 14
5
votes
2 answers

how to sent array data as formdata angular 4

I tried to post an array of data is not sending to server: webservice: deleteCategory() { return this.http.post('http://www.demo/webapi/deletecategory', { headers: { "Authorization": "Token " + this.token, …
linto johny
  • 137
  • 1
  • 2
  • 9
5
votes
1 answer

Angular Universal Prerendering with httpClient for getting data from /static/data.json content

I'm just trying to prerender an component with Angular Universal which fetches data with httpClient from the /static folder within the app itself. How do I use httpClient correctly in Angular Universal? Is it possible to access static assets from…
5
votes
3 answers

Angular2. Map http response to concrete object instance

Сommon approach to process http response is like that: return this._http.get(url) .map((res: Response) => res.json()); which provides you with an Observable where Object is dynamically created type from json…
rook
  • 2,819
  • 4
  • 25
  • 41
5
votes
2 answers

Angular HTTP: Status -1 and CORS

In our angular application sometimes we get http status -1 returned to us. The status -1 happens on a popup that is closed, so the user isn't affected by it, just our logs. I attempted to handle it by doing switch (response.status) { …
Mr. CatNaps
  • 121
  • 1
  • 8
5
votes
4 answers

Getting data from a web service with Angular.js

Im trying to get data in a Json format from a remote WS using Angular and im having some trouble. The data comes from the web service correctly but i cant use it inside the controller. Why is that? Angular Code: var booksJson; var app =…
Vandervidi
  • 642
  • 2
  • 14
  • 32
5
votes
1 answer

Wait for $http promise before the next request

I'm working on an angularJS app and this is my first website using this framework. In my app i've a need to make a $http call inside a for loop. With in the loop before the next iteration I want to wait for the response from my previous call. What…
Ajay Srikanth
  • 1,095
  • 4
  • 22
  • 43
5
votes
3 answers

Angular $http.get to localhost, always returns 404. 200 in browser

I can't create a successful get request in Angular 1.2.13. var getProgress = function() { var promise = $http({method: 'GET', url: 'http://localhost:8080/project/local/some/getStatus'}); promise.then(function(success) { …
dalvarezmartinez1
  • 1,385
  • 1
  • 17
  • 26
5
votes
1 answer

How do I set a conditional $resource timeout in angularjs?

It seems pretty clear how to set a global method to abort http requests. Something along the lines of $scope.canceller = $q.defer(); var API = $resource('http:/foo.com', {}, {get: { method:'GET', …
Greg Michalec
  • 849
  • 3
  • 10
  • 17
5
votes
1 answer

Angular JS cancel $http call before calling a new $http

In angular JS 1.1.5 you can cancel previously started $http calls. These two link1 and link2 gives some explanation on how it works. However I can't understand, how to use this in practice. I created plnkr to illustrate what I want to achieve. User…
5
votes
1 answer

Why AngularJS sends X-XSRF-TOKEN header as JSON string?

Angular sets the X-XSRF-TOKEN header to the value of the XSRF-TOKEN cookie: var xsrfValue = isSameDomain(config.url, $browser.url()) ? $browser.cookies()[config.xsrfCookieName || defaults.xsrfCookieName] :…
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746