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
0
votes
2 answers

$http and promises and POST

Given code like the following: function webCall() { return $http({ method: "POST", url: "http://destined/to/fail", data: {param1: 1}) .success(function(data, status) { return { test: "success!";} } ) …
Mike Cheel
  • 12,626
  • 10
  • 72
  • 101
0
votes
2 answers

Issue with angular $route.reload();

If have the following code but $route.reload() does not refresh the page. testControllers.controller('LogoutController', [ '$scope', '$http', '$route', function($scope, $http, $route) { $scope.logout = function() { …
gjerster
  • 13
  • 1
  • 1
  • 2
0
votes
1 answer

HTTP post with angular on a "before auth" laravel route

I authenticate my user only through laravel, so angular does not know about it. But I need to do a $http.post on a "before auth" route with angular in order to get some profil info: laravel route.php Route::post('profile', array('before' => 'auth',…
alskaa
  • 394
  • 1
  • 3
  • 14
0
votes
1 answer

Returned data from $http get on page load needs to be ready when $routeChangeSuccess event handler fires

My problem is, on page load, the data received from AJAX may not be ready when the $routeChangeSuccess event fires. In other words, I have a race condition between the event handler and AJAX. How do I ensure this data is there when the event fires…
Dean Or
  • 2,822
  • 2
  • 26
  • 25
0
votes
0 answers

AngularJS, cannot access $scope.variableName

I am new to AngularJS and having an strange problem, that I can't access to $scope.name. In the factory DataService I have a method getData() which uses $http.get(...). angular.module("app", []).factory("DataService", function($q, $http) { var…
0
votes
1 answer

How to deal with constantly changing data in Angular service $http call

I am creating an Angular.js app to consume the public Flickr API. I want to create a list/detail view of the most recent photographs on Flickr. I am using a factory in Angular to supply data to two controllers. Here is the factory…
0
votes
1 answer

Update scoped variable based on Angular $resource

I have an Angular app communicating with an external API. I'm able to generate the initial view from an Angular $resource call. My problem is that I have a form that runs a function on ng-click. That function then queries the API again and is…
wgp
  • 1,147
  • 15
  • 15
0
votes
0 answers

AngularJS second request on $http.get retrieves less data than when I do the request for first time

I am doing $http services, the size of the information that retrieves is variable, in average is between 300 Bytes and 6KBytes. I don't understand why it is different when I do the request for the first time, than when I do for the second time. When…
agusgambina
  • 6,229
  • 14
  • 54
  • 94
0
votes
1 answer

How to run xhr request with address written in template

I'm trying to run XHR request on URL that controller obtain from template. I get url like this Angular code: ManageControllers.controller('ManageCtrl', ['$scope', '$http', function ManageCtrl($scope, $http) { $scope.urls = { …
Sebastian Piskorski
  • 4,026
  • 3
  • 23
  • 29
0
votes
1 answer

Accessing and using JSON within an Angular service for logic flow

I asked the wrong question yesterday (and got a goodanswer that worked), but am realizing it's not what I needed. I need to be able to retrieve JSON data (preferably once), store it, and access it throughout my service. The challenge I'm having is…
0
votes
1 answer

Trying to store and access JSON from within service

I'm relatively new to Angular and trying to correct some functionality someone left us with. I have a service that retrieves JSON from another service, and then I'd like to use that JSON within the original service. I have it working when…
0
votes
1 answer

Sending Audio data through POST request in angular JS

I am using angular js to make POST request to API. I need to send some headers and audio file with it. I have written sample code for the same in controller. $scope.files='bostonSeltics.wav'; $scope.getText = function (access_token)…
SavantMohan
  • 35
  • 2
  • 9
0
votes
1 answer

Angular - Generate HTML file with JS and send to server

My goal is to generate an html file in javascript (based on form inputs), and use Angular's $http to send this generated file to the server (php) for storage. Is this possible? So far I've tried generating HTML strings on the client side and send…
Kahtaf Alam
  • 463
  • 2
  • 7
  • 19
0
votes
1 answer

AngularJS directive with $http requests (inprog error)

I've searched and tried many things for this, and I think maybe Im just doing it wrong. I have a single page app that has very similar DOM pieces, differentiated only by the data that is fed in to them from a service. Each DOM piece has a different…
oooyaya
  • 1,773
  • 1
  • 15
  • 39
0
votes
1 answer

How to get a custom made response on a request timed out

I'm not able to get my custom 503 response when a request is timed out. I am using the $http service and the timeout config is a promise. This is my plunker.
Eduardo
  • 63
  • 1
  • 8