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.

425 questions
196
votes
11 answers

How do I POST urlencoded form data with $http without jQuery?

I am new to AngularJS, and for a start, I thought to develop a new application using only AngularJS. I am trying to make an AJAX call to the server side, using $http from my Angular App. For sending the parameters, I tried the following: $http({ …
Veer Shrivastav
  • 5,126
  • 11
  • 46
  • 78
116
votes
6 answers

Using success/error/finally/catch with Promises in AngularJS

I'm using $http in AngularJs, and I'm not sure on how to use the returned promise and to handle errors. I have this code: $http .get(url) .success(function(data) { // Handle data }) .error(function(data, status) { //…
Joel
  • 7,162
  • 6
  • 50
  • 87
114
votes
16 answers

The 'Access-Control-Allow-Origin' header contains multiple values

I'm using AngularJS $http on the client side to access an endpoint of a ASP.NET Web API application on the server side. As the client is hosted on a different domain as the server, I need CORS. It works for $http.post(url, data). But as soon as I…
Papa Mufflon
  • 11,628
  • 5
  • 22
  • 30
61
votes
2 answers

Why are AngularJS $http success/error methods deprecated? Removed from v1.6?

The AngularJS documentation has a Deprecation Notice for the $http success and error methods. Is there a specific reason this abstraction was removed from the library?
philoniare
  • 1,483
  • 1
  • 11
  • 19
44
votes
9 answers

AngularJS not detecting Access-Control-Allow-Origin header?

I am running an angular app on a local virtualhost (http://foo.app:8000). It is making a request to another local VirtualHost (http://bar.app:8000) using $http.post. $http.post('http://bar.app:8000/mobile/reply', reply, {withCredentials: true}); In…
TaylorOtwell
  • 6,695
  • 6
  • 28
  • 40
33
votes
4 answers

AngularJS Uploading An Image With ng-upload

I am trying to upload a file in AngularJS using ng-upload but I am running into issues. My html looks like this:
Devin Dixon
  • 9,088
  • 20
  • 70
  • 136
31
votes
5 answers

SyntaxError: Unexpected token o at Object.parse (native) AngularJS

Question from AngularJS noob. I am trying to use an asmx web service to display grid. I tested the web service and it correctly outputs the JSON data. Here is my controller app.controller('SetupController', ['$scope', '$http', function ($scope,…
blue piranha
  • 3,101
  • 9
  • 44
  • 79
27
votes
9 answers

Error with $http.get in angularJS -- Success not a Function

Getting this error: angular.min.js:122 TypeError: $http.get(...).success is not a function at getUserInfo (app.js:7) at new (app.js:12) at Object.invoke (angular.min.js:43) at Q.instance (angular.min.js:93) at p…
Manzur Khan
  • 1,962
  • 3
  • 18
  • 37
19
votes
5 answers

How to mock an angular $http call and return a promise object that behaves like $http

Is there a way to return an HttpPromise (or something similar) to mimic a call to $http? I want to set a global variable that indicates whether the real HTTP request is made or whether a fake HttpPromise object is returned with fake data. For…
whyceewhite
  • 5,889
  • 7
  • 41
  • 50
19
votes
2 answers

How can we test non-scope angular controller methods?

We have few methods in Angular Controller, which are not on the scope variable. Does anyone know, how we can execute or call those methods inside Jasmine tests? Here is the main code. var testController = TestModule.controller('testController',…
16
votes
2 answers

How can I make $httpBackend insensitive to the order of URL query parameters?

I am using the Angular.js $httpBackend to test some services that wrap $http calls (this is in ngMock, not ngMockE2E). It seems that things like expect and when are sensitive to the order of URL query parameters. E.g. if I do…
15
votes
2 answers

What is the equivalent of jQuery ajax beforeSend in Angularjs?

I am familiar with Jquery AJAX call, which has different callbacks like beforeSend, success, complete, etc. This is the example AJAX call with Jquery: $.ajax({ url: 'register.php', type: 'POST', data: {name:name, email:email}, beforeSend:…
shasi kanth
  • 6,503
  • 22
  • 101
  • 151
14
votes
3 answers

Why do we prefer using $q in angular instead of $http

I am currently using $q service from angular to make API calls like this: var deferred = $q.defer(); $http.get(config.apiHost + details.url) .success(function (data) { deferred.resolve(data); }).error(function (msg) { …
Bhushan Goel
  • 1,992
  • 2
  • 16
  • 29
13
votes
4 answers

Getting AngularJS Error: "[$rootScope:inprog] $digest already in progress" without a manual $apply

Other posts on this error always include someone trying to $apply without using a safe apply, but that's not the case in my example. My function IS successfully returning the data I requested from the API, but I can't clean this bug and it's driving…
12
votes
3 answers

$filter is not a function AngularJS

app.controller('myController', ['$scope', '$http', '$filter', function($scope, $http, $filter) { The above is an example of my code where I am trying to use $http.get and also $filter inside my controller. The only problem is when I use it like so,…
1
2 3
28 29