A Secure Online Service from Utah.gov

Utah.gov

STAT 2.0

A Service from Utah.gov

Connecting...

STAT.ajax

AJAX Baked in goodness

STAT GET | stat.ajax.get(url, {params}, callback);

@param {string} url : path to endpoint
@param {object} params : params to send in URL
@callback {function} callback to run when readyState == 4

Example Params as object:

const aggParams = {
	username: 'utahgov',
	count: 4
}

Example with Callback:

stat.ajax.get('/aggregator/tweetsForUser.json', aggParams, ajax_callback);

This example may 404 if /aggregator/ is not avail in your domain path

TIP:

View source and/or console on this page to see an example GET request/response.

STAT POST | stat.ajax.post(url, {params}, callback);

@param {string} url : path to endpoint
@param {object} params : body
@callback {function} callback to run when readyState == 4

example:

stat.ajax.post(url, {name: 'bob', title: 'foo'}, function(xhr) {
	console.log(xhr.response);
});

Setting custom request headers

Setting a custom header is done by passing a specific object into the existing {params} object

@param {object} params: {setRequestHeader: {a:b, x:y}};

example:

const myParams: {
	location: 'foo',
	count: 10,
	setRequestHeader: {
		'x-captcha-verification': 'abcxyz0123',
		'x-another-custom-header': 'My Header'
	}
}

STAT.ajax will detect the 'setRequestHeader' object, strip it from the request params and apply it to the request header automatically.

Setting custom responseType

Setting a custom header is done by passing a specific key:value into the existing {params} object.

Note:

This is needed when a responsType other than JSON is provided by the response. Some browsers will break the response if the responseType doesn't match.

@param {object} params: {responseType: 'text'};

example:

const myParams: {
	location: 'foo',
	count: 10,
	responseType: 'text'
}

STAT.ajax will detect the 'responseType' key, strip it from the request params and apply it to the request automatically.