DoneJS StealJS jQuery++ FuncUnit DocumentJS
5.33.3
6.0.0 4.3.0 3.14.1 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-bind
      • can-compute
      • can-debug
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-compat
      • can-map-define
      • can-observable-array
      • can-observable-object
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Views
      • can-attribute-observable
      • can-component
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-element
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • steal-stache
    • Data Modeling
      • can-connect
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-tag
      • can-fixture
        • types
          • Store
          • ajaxSettings
          • request
          • requestHandler
          • response
        • properties
          • delay
          • on
          • rand
          • store
      • can-fixture-socket
      • can-local-store
      • can-memory-store
      • can-ndjson-stream
      • can-query-logic
      • can-realtime-rest-model
      • can-rest-model
      • can-set-legacy
      • can-super-model
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-define-validate-validatejs
      • can-type
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

response

  • Edit on GitHub

Used to detail a response.

response( [status], body, [headers], [statusText] )

Using the response function to return an unauthorized error.

import {fixture, ajax} from "can";

fixture( {url: "/todos/{action}"},
  ( request, response, headers, ajaxSettings ) => {
    response(
      401,
      { message: "Unauthorized" },
      { "WWW-Authenticate": "Basic realm=\"myRealm\"" },
      "unauthorized"
    );
  }
);

// fixture response log {
//   headers: {WWW-Authenticate: "Basic realm='myRealm'"},
//   responseBody: {message: "Unauthorized"},
//   statusCode: 401,
//   statusText: "unauthorized"
// }

ajax( {type:"POST", url: "/todos/delete"} ).catch( error => {
  console.log(error); //-> {message: "Unauthorized"}
} );

The default statusText will be "ok" for statuses between 200 and 300 including 200, and 304. It will "error" for everything else.

Parameters

  1. status {Number}:

    The HTTP response code. Ex: 200.

  2. body {Object|String}:

    A JavaScript object, or a string that will be serialized and set as the responseText of the XHR object, or the raw string text that will be set as the responseText of the XHR object.

  3. headers {Object}:

    An object of HTTP response headers and values.

  4. statusText {String}:

    The status text of the response. Ex: "ok" for 200.

response( body )

Using the body object to return a single message. The status will be 200 and statusText will be "ok".

import {fixture, ajax} from "can";

fixture( {url: "/todos"}, ( request, response ) => {
  // Just body
  response( { message: "Hello World" } );
});

ajax( {url: "/todos"} ).then( result => {
  console.log( result ); //-> {message: "Hello World"}
} );

Parameters

  1. body {Object|String}:

    A JavaScript object, or a string that will be serialized and set as the responseText of the XHR object, or the raw string text that will be set as the responseText of the XHR object.

response( [status], body )

Response will always return a 401 status. The statusText will be "error".

import {fixture, ajax} from "can";

fixture( {url: "/todos"}, ( request, response ) => {
  // status and body
  response( 401, { message: "Unauthorized" } );
});

ajax( {url:"/todos"} ).catch( error => {
  console.log( error ); //-> 401, {message: "Unauthorized"}
});

Parameters

  1. status {Number}:

    The HTTP response code. Ex: 200.

  2. body {Object|String}:

    A JavaScript object, or a string that will be serialized and set as the responseText of the XHR object, or the raw string text that will be set as the responseText of the XHR object.

response( body, [headers] )

Response will return the responseBody with custom headers. Because it's not set the status will default to 200, and the statusText to "ok".

import {fixture, ajax} from "can";

fixture( {url: "/todos"}, ( request, response ) => {
  // body and headers
  response(
    "{\"message\":\"Unauthorized\"}",
    { "WWW-Authenticate": "Basic realm=\"myRealm\"" }
  );
});

ajax( {url: "/todos"} ).then( result => {
  console.log( result ); //-> {message: "Unauthorized"}
} );

Parameters

  1. body {Object|String}:

    A JavaScript object, or a string that will be serialized and set as the responseText of the XHR object, or the raw string text that will be set as the responseText of the XHR object.

  2. headers {Object}:

    An object of HTTP response headers and values.

response( [status], body, [statusText] )

Response will return with the given status and statusText along with the responseBody.

import {fixture, ajax} from "can";

fixture( {url: "/todos"}, ( request, response ) => {
  // status, body, statusText
  response( 401, "{\"message\":\"Unauthorized\"}", "unauthorized" );
});

ajax( {url: "/todos"} ).catch( error => {
  console.log(error); //-> {message: "Unauthorized"}
} );

Parameters

  1. status {Number}:

    The HTTP response code. Ex: 200.

  2. body {Object|String}:

    A JavaScript object, or a string that will be serialized and set as the responseText of the XHR object, or the raw string text that will be set as the responseText of the XHR object.

  3. statusText {String}:

    The status text of the response. Ex: "ok" for 200.

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 5.33.3.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news