Skip to content

Thrunium / Reference

ajax

Sends an asynchronous HTTP request and returns its XMLHttpRequest. The answer arrives later, in the success or error function of the options.

General

ajax(data)

  • data AjaxOptions

    The request options.

  • data.url string

    The URL to send the request to.

  • data.type string

    HTTP method, such as 'get' or 'post'.

  • data.data Document | XMLHttpRequestBodyInit | nulloptional

    Request body, such as a form-encoded string ('name=value&other=value') or FormData. Leave it out for a request without a body.

  • data.success (x: XMLHttpRequest) => voidoptional

    Called with the XMLHttpRequest when the request finishes with status 200; read the answer from its response or responseText. Default logs the request to the console.

  • data.error (x: XMLHttpRequest) => voidoptional

    Called with the XMLHttpRequest when the request finishes with any other status, including network errors (status 0). Default does nothing.

  • data.contentType string | falseoptional

    Value of the Content-Type header, or false to send none (needed for FormData). Default is 'application/x-www-form-urlencoded'.

  • data.withCredentials booleanoptional

    Set to true to send cookies and other credentials with a cross-site request. Default is false.

returns XMLHttpRequest

Sends an asynchronous HTTP request and returns its XMLHttpRequest. The answer arrives later, in the success or error function of the options.

JavaScript
THRUNIUM.ajax({
	url: 'https://www.example.com/get-data',
	type: 'get',
	withCredentials: true,
	success: function(request) {
		console.log(request.responseText);
	},
	error: function(request) {
		console.log('Request failed with status ' + request.status);
	}
});
JavaScript
THRUNIUM.ajax({
	url: 'https://www.example.com/score',
	type: 'post',
	data: 'name=Ann&score=120',
	success: function(request) {
		console.log('Saved');
	}
});