Object
This object is used to match values on request objects.
If there's a match, the fixture handler provided with the
ajaxSettings will be invoked.
If a property on an ajaxSettings
is not provided, all request values
will be matched for that property.
For example,
you can match all GET
requests, no matter what url
is passed like:
import {fixture, ajax} from "can";
fixture({method: "GET"}, () => {
return "success";
});
// randomly selects '/example', '/canJS', or '/random'.
const url = fixture.rand(["/example", "/canJS", "/random"], 1);
ajax( {url} ).then(result => {
console.log( result ); //-> "success"
});
Options
-
url
{String}
:
The requested url with anything after the querystring taken off in GET
and DESTROY
method requests. For example, you can't match:
fixture({method: "GET", url: "/things?name=Justin"});
Instead write:
import {fixture, ajax} from "can";
fixture({method: "GET", url: "/things", data: {name: "Justin"}}, () => {
return "success";
} );
// can also be: `$.get("/things", {name: "Justin"})`
// attempting to just get from the endpoint "/things" won't work.
ajax( {url: "/things?name=Justin"} ).then( result => {
console.log( result ); //-> "success"
} );
The url
can have templates like:
import {fixture, ajax} from "can";
import "//unpkg.com/jquery@3.3.1/dist/jquery.js";
fixture({method: "GET", url: "/things/{id}"}, () => {
return "success";
} );
// attempting to just get from the endpoint "/things" won't work.
ajax( {url: "/things/1"} ).then( result => {
console.log( result ); //-> "success"
} );
The templated values get added to the request object's data
.
-
method
{String}
:
The method of the request. Ex: GET
, PUT
, POST
, etc. Capitalization is ignored.
-
data
{Object}
:
Match the data of the request. The data of the querystring or the data to XMLHTTPRequest.prototype.send
is converted to a JavaScript objects with either JSON.stringify
or can-deparam. The data must match part of the request
's data.
-
async
{Boolean}
:
Write true
to match asynchronous requests only.