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
4
votes
1 answer

Getting JSON for Angular 2 from HTTP remote server fails, but succeeds localy

I'm trying to read a JSON file using htttp.get in Angular2. When I use a file stored localy on my project, it works OK. But - when I use a remote server, with exactly the same file, I get the following error: 404 - Not Found Collection 'undefined'…
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
4
votes
2 answers

Angular 2 Http RetryWhen

I'm trying to use retryWhen in HTTP calls. It works perfectly when try to use like this: return this.http.get(`${environment.apiUrl}/track/${this.user.instance._id}/${this.currentPlayer.playlist.id}/next?s=${this.playerCounter}`, options) …
Paulo GR
  • 51
  • 1
  • 9
4
votes
2 answers

Angular 2 Spring Boot Login CORS Problems

I am trying to develop an application with Spring Boot for the back end and Angular 2 for the front end. Actually i have connection problems, when i accessing on the mvc login page from spring. I get following Problems: ( Console ) ( Angular 2…
Muhammed Misir
  • 400
  • 9
  • 22
4
votes
1 answer

converting an ajax request to angular $http

I'm learning how to use angular and I'm not really that familiar with making request to an api. I'm trying to practice using http://api.football-data.org/index. The json data I wanted to get from my angular factory is…
Rawle Juglal
  • 395
  • 4
  • 17
4
votes
2 answers

Always do action before and after $http

I want my app to always do some actions before and after $http running like loading animation or delete all messages. // Add loading animation + Remove all messages $http({}).then(function(response) { // Remove loading animation + Add Success…
4
votes
3 answers

Angular http and spring boot rest service

I am calling a spring boot REST service from my angular UI. It worked good as long as the Spring Boot Rest service was executed as a Spring boot App. But once i converted it to a WAR file and deployed on a Jboss 6.2.4 server, i am getting 404. I see…
tindu edward
  • 299
  • 1
  • 3
  • 17
4
votes
1 answer

Angular 2 Http – How to Get JSON Data from API with finance_charts_json_callback() callback

I'm trying to get json data from this api: http://chartapi.finance.yahoo.com/instrument/1.0/NFLX/chartdata;type=quote;range=1d/json And I don't know how to get into the returned finance_charts_json_callback(). I'm using Angular 2's…
Hunter
  • 405
  • 2
  • 5
  • 10
4
votes
4 answers

How to make controller wait untill app.run finishes

In my application, I'm getting some data in app.run using $http.get() The result from this is required to proceed to the controller. Currently my controller is executing before the completion of this $http.get How can I make my controller's execute…
Jojo
  • 403
  • 5
  • 16
4
votes
1 answer

AngularJS's $http.post() sends each time two AJAX requests, one without the data and one with

I have a weird problem with AngularJS's $http service which, as far as I can see, no one else has. Every time I use $http.post() to sent cross domain requests, I can see in the developer tools (network pane) that two different requests are being…
Reuven
  • 93
  • 1
  • 5
4
votes
2 answers

AngularJS $http redirect on status code

I have a angular application with many $http request and i want redirect the users on the login page if the server session expires (get 401). Does anyone know a solution witch works for all $http without adding .error() on every $http?
Christoph
  • 1,993
  • 1
  • 25
  • 33
4
votes
3 answers

$state.go not working from promise

I am trying to implement a basic security check to block users from accessing certain states depending on their permission set: 'use strict'; var autoExecApp = angular.module("myApp", ['ui.router']); autoExecApp.config(['$stateProvider',…
Alex
  • 2,405
  • 4
  • 23
  • 36
4
votes
1 answer

jQuery AJAX call to Web API returns Status 240 when the API Controller Action returns BadRequest()

We have a Web API that has an action that returns BadRequest() which is a 400. the call to the API looks like this: $.post("/api/controller/action", {test:"Bad data"}).success(function(data){ console.log(data) }); the API Controller Action…
Victorio Berra
  • 2,760
  • 2
  • 28
  • 53
4
votes
1 answer

Proper use of transformers vs interceptors

When POSTing to an endpoint in a service layer to update a user's profile, I need to strip certain values from the request payload (the profile with the desired modifications from the client) and re-attach them in the response payload (the updated…
4
votes
2 answers

Angular 1.3.0 honors JSON responses, how do I override this or pre-parse the response before Angular?

In the latest release of Angular (v1.3.0) they added a fix for the content-type header for application/json. Now all my responses get an error cause they are not valid JSON. I know I should change the back-end to respond with a plain text header,…
KungWaz
  • 1,918
  • 3
  • 36
  • 61
4
votes
3 answers

Best way to provide url to angular $http.get/.post

I am using angular with MVC4. I would like to call a MVC controller method from javascript using $http.get(). It requires a url (obviously). I would like this to be a relative path, but how do I write that? My controller is in…
Shumii
  • 4,529
  • 5
  • 32
  • 41