Questions tagged [angularjs-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.

426 questions
12
votes
2 answers

Angular Js - set token on header default

Im trying to add a header with my access token to each API call. It works well for all the GET requests, but as soon as I try to make a POST the header isn't added. Here is how I add the token: app.factory('api', function ($http, $cookies) { return…
razeth01
  • 638
  • 2
  • 11
  • 25
11
votes
1 answer

AngularJs get blob with $http.get and extract type from response

I have webApi return me a blob file that I have to display. how can I know what type is the blob that I got? It could be anything, pdf, doc, jpeg etc. $http({ method: 'GET', url: (itemsUrl), responseType: 'arraybuffer' }).then(function mySucces(res)…
rikush
  • 520
  • 1
  • 6
  • 20
8
votes
2 answers

AngularJS : returning data from service to controller

I am trying to create a service to get json and pass it to me homeCtrl I can get the data but when a pass it to my homeCtrl it always returns undefined. Im stuck. My Service: var myService = angular.module("xo").factory("myService", ['$http',…
8
votes
1 answer

Testing backend API via $http in AngularJS/karma/jasmine tests?

How do I test my API backend using AngularJS/karma/jasmine tests? I have tried to create the smallest test-case showing my error: echo_server.py from bottle import response, route, run @route('/echo/') def echo_echo(echo): …
A T
  • 13,008
  • 21
  • 97
  • 158
7
votes
2 answers

CellFilter is getting called before $http Response in UI-Grid

I am using ui-grid to bind data from Role Table which contains Department Id as PrimaryKey. I am calling Web Api to get all the roles in the table and show in ui-grid. Department Table Role Table My real problem is that I want to convert…
7
votes
4 answers

Angular: $http requests gives -1 status

A node.js server gives "This is a string". (writeHeader, write( string), end). When performing a $http request, I see that the node.js server is responding and sending the information back. In Angular I perform the following request:…
tm1701
  • 7,307
  • 17
  • 79
  • 168
7
votes
4 answers

How to pass client-side parameters to the server-side in Angular/Node.js/Express

Probably a very basic question, but I cannot seem to find a simple answer. I have a GET method leveraging Angular's $http that is requesting a promise from a particular url (URL_OF_INTEREST). On this server, I run an express script server.js script…
WJA
  • 6,676
  • 16
  • 85
  • 152
7
votes
5 answers

AngularJS, $http.get() and "controller as"

I'm pretty new to the whole AngularJS world and how it works, however I am struggling to get it working as expected. I know that its something to do with the way I am using $http.get() and trying to assign the variables back to my controller, but I…
Doug
  • 547
  • 10
  • 23
7
votes
1 answer

How to test $http without mock with jasmine and angular-mocks?

I want to make an integration test with real calls to my server, so, I don't want to use the $httpBackend module from angular-mocks, So I try this: beforeEach(inject(function($rootScope,_MembersDataSvc_){ service =…
cmarrero01
  • 691
  • 7
  • 21
7
votes
2 answers

expectHEAD is documented but not implemented?

In our internal angularjs project, one of the services has $http.head() call which I'm trying to test. For testing, I'm using Fake HTTP backend provided by angular-mocks. Here is the relevant code: it('handle status code 200', inject(function…
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
7
votes
3 answers

Pushing or Unshifting into the transformRequest array of $http (non-global)

See the accepted answer to here for a pretty good explanation on the transFormRequest function/array. In the answer's last example: var transform = function(data){ return $.param(data); } $http.post("/foo/bar", requestData, { headers: {…
NicolasMoise
  • 7,261
  • 10
  • 44
  • 65
6
votes
1 answer

Angular JS TypeError: $http is not a function

I have read through all the posts where people get this issue where $http is not a function, and it looks like for the most part it is due to injections being done in the wrong order. My module definition looks like this: angular.module("app",…
6
votes
2 answers

AngularJS $http.get async execution order

I recently did a lot of coding in AngularJS. After some time it started to feel comfortable with it and also got really productive. But unfortunately there is this one thing I don't understand: Within my project I need to get data through $http.get…
J.S.
  • 63
  • 1
  • 5
6
votes
2 answers

AngularJS : controller scope won't sync with promise

I've picked up a project and I'm trying to return some data from a service to my controller. I've been at this for about 12 hours, and have tried different methods. They all usually result in this same kind of 'missing data'. I've tried using…
6
votes
1 answer

How to add headers to $http for only certain domains

Is it possible to set a header only for specified domains? The "normal" way of doing this adds that header for all calls, even calls that retrieve HTML templates for example. $http.defaults.headers.common['Authorization']='...'; So far, other…
Francisc
  • 77,430
  • 63
  • 180
  • 276
1
2
3
28 29