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

How to invalidate $http call with cache:true when header changes

I'm fetching a remote resource and caching the result: $http({ method:'GET', cache:true, url:'...' }); This works fine. However, when the user changes the languge in the UI, I also change the Accept-Language header on all AJAX…
Francisc
  • 77,430
  • 63
  • 180
  • 276
0
votes
1 answer

AngularJS: Unexpected undefined in chained results

I've come across this issue before with nested directives, but I managed to find a workaround there. I have code that looks a bit like, var token = API.callGeneric({}, {method: 'kds.getTokenExtended2', params: ['demo', 'demo', '', '', '', '', '',…
Ashesh
  • 2,978
  • 4
  • 27
  • 47
0
votes
1 answer

AngularJS: issues creating an API request

I have a scenario in which I have to deal with a misconfigured server, which only understands a stringified JSON. What works with the server is: var req = JSON.stringify({id: 0, method: 'getToken', params: ['something', 'password',…
Ashesh
  • 2,978
  • 4
  • 27
  • 47
0
votes
0 answers

AngularJS $http.post headers

I am trying to do a http post, but I cant make it work, I am having a 405 Method Not Allowed, but when I do with a plugin of Chrome works perfectly post: function(userData){ var promise = $http.post(url, userData) .success(function (data,…
agusgambina
  • 6,229
  • 14
  • 54
  • 94
0
votes
2 answers

Why promise doesnt work as expected in AngularJS

In my AngularJS application on every request to change the page i run : $rootScope.$on('$locationChangeStart', function (event, next, current) { var user; $http.get('/api/Authentication/UserAuthenticated').then(function (data) { …
Timsen
  • 4,066
  • 10
  • 59
  • 117
0
votes
1 answer

Angularjs service does not perform more than one http request

I have a problem with a service angularjs , do not make more than one http request . The service : .factory('checkpoints', function($http, user_auth) { var promise = $http({ method: "POST", url: remote_ws…
0
votes
2 answers

AngularJS $http loading screen compatibility

I've used the following angular module to create a loading screen until all $http requests finish. It works fine on angular 1.0.7, but doesn't work on angular 1.2.10 . I need to use angular-resource, and angular-route also, so I can't stay on…
Photovor
  • 403
  • 1
  • 6
  • 17
0
votes
1 answer

AngularJS request interceptor causing trouble to my templateUrl view resolution

I would like to prepend the value of my
balteo
  • 23,602
  • 63
  • 219
  • 412
0
votes
1 answer

How to ensure that angular $resource uses the following angular $http settings?

I am using CakePHP as my backend. Therefore, if I use $http, I need to have the following: a) the default config must be angularApp.config(function ($httpProvider) { $httpProvider.defaults.headers.common['Content-Type'] =…
Kim Stacks
  • 10,202
  • 35
  • 151
  • 282
0
votes
1 answer

Angular - How to Project $http Response

I want to project the result of my $http call into another model in order to make the projection global to the service call. In other words, the result i get from my http/api call is not using the exact model i want. How do i do this projection…
Micah
  • 10,295
  • 13
  • 66
  • 95
0
votes
1 answer

Angular jsonp is not workin

I'm trying to making jsonp call using below code but doesn't seems to be working for me. Code var url = 'http://z:15957/Category/Categories?callback=JSON_CALLBACK'; $http.jsonp(url).success(function (data) { $scope.results =…
user1968030
-1
votes
1 answer

Promise Chaining is not working as expected

I am trying use the promise chaining for the $http.get calls I am making from my angular application $http.get(url, config) .then(newViewRequest) .then(function (response) { // success async $scope.viewRequest1.data = response.data; …
trx
  • 2,077
  • 9
  • 48
  • 97
-1
votes
2 answers

Angular-js $scope variable is not updated in view

I am new to angular-js and building a simple to-do application. I am creating a task and displaying the created task in a html table using ng-repeat. But the problem is that after posting the data, $scope.tasks variable is updated on controller…
Sampath
  • 19
  • 1
  • 5
-1
votes
1 answer

AngularJS HTTP request error handling

I've seen different examples of Restful API facotires error handling and this is a bit confusing. Here is two examples: A angular.module('app').controller('userCtrl', function($scope, User) { $scope.users = []; $scope.error = ''; …
Jis0
  • 91
  • 11
-1
votes
2 answers

AngularJS - Make synchronous http request

Angular Service this.sendCommand = function (someData) { URL = myURL; return $http({ method: 'POST', url: URL, data: someData …
Slimshadddyyy
  • 4,085
  • 5
  • 59
  • 121
1 2 3
28
29