Mastering Web Application

(Rick Simeone) #1
Chapter 3

First of all we can see that there is a dedicated method to issue XHR GET requests.
There are equivalent methods for other types of XHRrequests as well:



  • GET: $http.get(url, config)

  • POST: $http.post(url, data, config)

  • PUT: $http.put(url, data, config)

  • DELETE: $http.delete(url, config)

  • HEAD: $http.head


It is also possible to trigger a JSONP request with $http.jsonp(url, config).


The parameters accepted by the $http methods differ slightly depending on the
HTTP method used. For calls that can carry data in their body (POST and PUT) the
method signature is the following one:



  • url: the URL to be targeted with a call

  • data: data to be sent with a request's body

  • config: a JavaScript object containing additional configuration options
    influencing a request and a response


For the remaining methods (GET, DELETE, HEAD, JSONP) where there is no data to
be sent with the request's body, the signature becomes simpler and is reduced to two
parameters only: url and config.


The object returned from the $http methods allows us to register success and
error callbacks.


The configuration object primer

The JavaScript configuration object can hold different options influencing the request,
response and data being transmitted. The configuration object can have the following
properties (among others):



  • method: HTTP method to be issued

  • url: URL to be targeted with a request

  • params: parameters to be added to the URL query string

  • headers: additional headers to be added to a request

  • timeout: timeout (in milliseconds) after which a XHR request will be dropped

  • cache: enables XHR GET request caching

  • transformRequest, transformResponse: transformation functions that
    allows us to pre-process and post-process data exchanged with a back-end

Free download pdf