Class: Response

caph.xhr. Response

new Response(xhr, type)

A wrapping class of the browser's native XMLHttpRequest object.

Parameters:
Name Type Description
xhr XMLHttpRequest

The browser's native XMLHttpRequest

type String

The type of returned data from server.

Members

value :Object

The value which is converted to requested type is saved.

Type:
  • Object
Since:
  • 2.0.0
Example
caph.xhr('/test').then(function(response) {
     console.log(response.value);
 });

Methods

getHeader(name) → {String}

Gets the string containing the text of the specified header.

Parameters:
Name Type Description
name String

A header name to search for.

Since:
  • 2.0.0
Returns:
Type
String
Example
caph.xhr('/test').then(function(response) {
     console.log(response.getHeader('Accept'));
 });

getJSON() → {Object}

Gets the response to the request as JSON.

Since:
  • 2.0.0
Returns:
Type
Object
Example
caph.xhr('/test', {
     type: 'JSON'
 }).then(function(response) {
     console.log(response.getJSON());
 });

getStatus() → {Number}

Gets the status of the response to the request. This is the HTTP result code.

Since:
  • 2.0.0
Returns:
Type
Number
Example
caph.xhr('/test').then(function(response) {
     console.log(response.getStatus());
 });

getStatusText() → {String}

Gets the response string returned by the HTTP server. This includes the entire text of the response message.

Since:
  • 2.0.0
Returns:
Type
String
Example
caph.xhr('/test').then(function(response) {
     console.log(response.getStatusText());
 });

getText(escape) → {String}

Gets the response to the request as string.

Parameters:
Name Type Description
escape Boolean

A Boolean indicating whether to escape the string.

Since:
  • 2.0.0
Returns:
Type
String
Example
caph.xhr('/test', {
     type: 'TEXT'
 }).then(function(response) {
     console.log(response.getText());
 });
 

getType() → {String}

Gets the type of returned data from server.

Since:
  • 2.0.0
Returns:
Type
String
Example
caph.xhr('/test').then(function(response) {
     console.log(response.getType());
 });

getXML() → {XMLDocument}

Gets the response to the request as XML.

Since:
  • 2.0.0
Returns:
Type
XMLDocument
Example
caph.xhr('/test', {
     type: 'XML'
 }).then(function(response) {
     console.log(response.getXML());
 });

isSuccess() → {Boolean}

Determines whether the request is completed successfully. When the return status value is 0, it is considered success status. Because if you request a local file, it will return 0 status value. So It is strongly recommended that check the real return value.

Since:
  • 2.0.0
Returns:
Type
Boolean
Example
caph.xhr('/test').then(function(response) {
     if (reponse.isSuccess()) {
         console.log(response.value);
     }
 });