Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Latest commit

 

History

History
79 lines (62 loc) · 3.57 KB

ajax.md

File metadata and controls

79 lines (62 loc) · 3.57 KB

Rx.DOM.ajax(url)

Rx.DOM.ajax(settings)

Creates an observable for an Ajax request with either a settings object with url, headers, etc or a string for a URL.

Arguments

  • url (String): A string of the URL to make the Ajax call.

OR

  • settings (Object): An object with the following properties

    • async (Boolean): Whether the request is async. The default is true.
    • body (Object): Optional body
    • crossDomain (Boolean): true if to use CORS, else false. The default is false.
    • withCredentials (Boolean): true if to use CORS withCredentials, else false. The default is false.
    • headers (Object): Optional headers
    • method (String): Method of the request, such as GET, POST, PUT, PATCH, DELETE. The default is GET.
    • password (String): The password for the request.
    • progressObserver (Observer): An optional Observer which listen to XHR2 progress events or error timeout values.
    • responseType (String): The response type. Either can be 'json', 'text' or 'blob'. The default is 'text'
    • timeout: Number - a number representing the number of milliseconds a request can take before automatically being terminated. A value of 0 (which is the default) means there is no timeout.
    • url (String): URL of the request
    • user (String): The user for the request.

Returns

(Observable): An Observable sequence with the following data.

For a successful operation, the result will contains the following:

  • response - (Object): The response from the XmlHTTPRequest. Parsed into JSON if the responseType set.
  • status - (Number): The HTTP status code.
  • responseType - (String): The HTTP Response type.
  • xhr - (XmlHTTPRequest): The XmlHTTPRequest from the request.
  • originalEvent - (Object): The original event from the callback handler.

For a failed operation, the result will contain the following:

  • type - (String): The type of rejection. This will be either 'error' or 'abort'.
  • status - (Number): The HTTP status code.
  • xhr - (XmlHTTPRequest): The XmlHTTPRequest from the request.
  • originalEvent - (Object): The original event from the callback handler.

Example

The following example uses a simple URL to retrieve a list of products.

Rx.DOM.ajax({ url: '/products', responseType: 'json'})
  .subscribe(
    function (data) {
      data.response.forEach(function (product) {
        console.log(product);
      });
    },
    function (error) {
      // Log the error
    }
  );

Location

File:

Dist:

Prerequisites:

NPM Packages:

NuGet Packages:

Unit Tests: