In normal JavaScript we use xmlhttp for Ajax. Angular is providing ajax service using $http. Is there anything extra things in $http compare to xmlhttp?
$http in AngularJS cannot be configured to be synchronous while xmlhttp can do either synchronous or asynchronous. In AngularJS world, we use $http because it is the "Angular way" of doing requests.
Related
Currect Structure
I'm Bootstrapping my angular application manually because I have to get some data before booting my angular app, which uses $http & injected it manually using:
var initInjector = angular.injector(['ng']);
var $http = initInjector.get('$http');
& use $http to call api
I also have an http interceptor which uses $httpProvider to handle specific errors in app.config function.
$httpProvider.interceptors.push('HttpInterceptor');
The Problem
Consider the api call before bootstrapping, the http interceptor handling is not working. I think because the factory i'm passing the $httpProvider is not yet tied up & will be initialised after the bootstrapping is done.
So Tell Me
Should I handle error case manually for that particular http request(before bootstrapping) which I think would be redundant or Is there a way I can make tie the httpInterceptor work before bootstrapping.
What would be a better approach?
Can I use either for a REST call ? Or is there some minor difference ? I have used both of them in my projects with no side effects,
These are the main differences between $http and $resource:
$http:
$http is for universal purpose. It is an Ajax call.
$http is built into the AngularJS framework.
$http is good for quick retrieval.
$http is injected directly into an AngularJS controller by the developer.
For more details about $http refer: https://docs.angularjs.org/api/ng/service/$http
$resource:
$resource wraps $http to use in RESTful web APIs.
$resource needs to add the module separately.
$resource is good for conditions slightly more complex than $http.
$resource does not allow us to do much with data once it is consumed in the application. It is in the final state when it is delivered to the HTML DOM. The data are the same that the $http method will receive.
For more details about $resource refer: https://docs.angularjs.org/api/ngResource/service/$resource
You can find more answer here and here.
What's the difference between using $resource to make a put request and using $http.put to make a put request? Does it make any difference in the speed or performance?
Check Answers in this stackoverflow thread
$resource is further high-level abstraction of $http. I don't think $resource added significant performance burden on top of $http to get the high-level API .
The Angularjs tutorial shows something using the $http service and then testing that using the $httpBackend mock. What it doesn't explain is why you mock $httpBackend and not just mock the $http service itself? Can anyone shed light of this?
You don't mock $httpBackend. You use it to mock the actual HTTP requests that $http makes. I suppose you probably could just mock $http itself, but $httpBacked provides a lot of functionality for asserting certain requests are made (The expect methods) and for just dummying in a response (The when methods). In short, $httpBacked makes testing code that uses $http much much easier.
I've browsed through the documentation but couldn't find anything mentioning this. I'm using Angular's $http service and want to run code before every ajax event, and after every ajax event (to display a loading message while waiting for the server to respond).
I have jQuery included and tried doing:
$(document).ready(function() {
$('body').ajaxSend(function(event, jqXHR) {
console.log('in jquery send');
}) ;
});
But couldn't get the message logged to the console. Any help is appreciated.
Please check out the docs on $http service: http://docs.angularjs.org/api/ng.$http
You will see that there are two options that you can use:
httpInterceptor (see Response interceptors section in the docs) - it allows you to wrap every response and do whatever you want before it completes the request. It uses promises so it's really easy to even do some async stuff after each request.
request/response transformations (see Transforming Requests and Responses) - it allows you to process every request and response that goes through $http - so that would be my choice as it allows you to hook before and after the request.