ajaxindex.mjsline 395
A module for the Ajax request.
If the browser supports Promise, return the Promise object. If not, return null.PARAMETERS
Name Type Description options
Object
Options for the Ajax request
PROPERTIES
Name Type Description url
string
URL string
method
'GET''POST''PUT''DELETE''PATCH''OPTIONS''HEAD'
Method of the Ajax request
contentType
[ string ]
Content-Type for the Ajax request. It is applied to POST, PUT, and PATCH requests only. Its encoding automatically sets to UTF-8.
params
[ * ]
Parameters to send by the Ajax request
serializer
[ serializer ]
Serializer that determine how to serialize the parameters. Default serializer is serialize().
beforeRequest
[ beforeRequestCallback ]
beforeRequest callback executed before the Ajax request is sent.
success
[ successCallback ]
success callback executed when the response is received successfully.
error
[ errorCallback ]
error callback executed when the request failed.
complete
[ completeCallback ]
complete callback executed when the request is completed after success or error callbacks are executed.
withCredentials
[ boolean ]
Determine whether cross-site Access-Control requests should be made using credentials or not. This option can be used on IE10+
mimeType
[ string ]
Override the MIME type returned by the server. This options can be used on IE11+
EXAMPLES
import ajax from 'tui-code-snippet/ajax'; // import ES6 module (written in ES6) // import ajax from 'tui-code-snippet/ajax/index.js'; // import transfiled file (IE8+) // var ajax = require('tui-code-snippet/ajax/index.js'); // commonjs // If the browser supports Promise, return the Promise object ajax({ url: 'https://nhn.github.io/tui-code-snippet/', method: 'POST', contentType: 'application/json', params: { version: 'v2.3.0', author: 'NHN. FE Development Lab <dl_javascript@nhn.com>' }, success: res => console.log(`success: ${res.status} ${res.statusText}`), error: res => console.log(`error: ${res.status} ${res.statusText}`) }).then(res => console.log(`resolve: ${res.status} ${res.statusText}`)) .catch(res => console.log(`reject: ${res.status} ${res.statusText}`)); // If the request succeeds (200, OK) // success: 200 OK // resolve: 200 OK // If the request failed (503, Service Unavailable) // error: 503 Service Unavailable // reject: 503 Service Unavailable // If the browser does not support Promise, return null ajax({ url: 'https://ui.toast.com/fe-guide/', method: 'GET', contentType: 'application/json', params: { lang: 'en' title: 'PERFORMANCE', }, success: res => console.log(`success: ${res.status} ${res.statusText}`), error: res => console.log(`error: ${res.status} ${res.statusText}`) }); // If the request succeeds (200, OK) // success: 200 OK // If the request failed (503, Service Unavailable) // error: 503 Service Unavailable
Static Properties
defaults:
index.mjsline 439
Default configuration for the Ajax request.
PROPERTIES
Name Type Description baseURL
string
baseURL appended with url of ajax options. If url is absolute, baseURL is ignored.
ex) baseURL = 'https://nhn.github.io', url = '/tui.code-snippet' => request is sent to 'https://nhn.github.io/tui.code-snippet'
ex) baseURL = 'https://nhn.github.io', url = 'https://ui.toast.com' => request is sent to 'https://ui.toast.com'headers
Object
request headers. It extends the header object in the following order: headers.common -> headers[method] -> headers in ajax options.
serializer
serializer
Serializer that determine how to serialize the parameters. If serializer is specified in both default options and ajax options, use serializer in ajax options.
beforeRequest
beforeRequestCallback
beforeRequest callback executed before the Ajax request is sent. Callbacks in both default options and ajax options are executed, but default callbacks are called first.
success
successCallback
success callback executed when the response is received successfully. Callbacks in both default options and ajax options are executed, but default callbacks are called first.
error
errorCallback
error callback executed when the request failed. Callbacks in both default options and ajax options are executed, but default callbacks are called first.
complete
completeCallback
complete callback executed when the request is completed after success or error callbacks are executed. Callbacks in both default options and ajax options are executed, but default callbacks are called first.
EXAMPLES
ajax.defaults.baseURL = 'https://nhn.github.io/tui.code-snippet'; ajax.defaults.headers.common = { 'Content-Type': 'application/json' }; ajax.defaults.beforeRequest = () => showProgressBar(); ajax.defaults.complete = () => hideProgressBar();
Static Methods
delete(url, options)index.mjsline 541
Send the Ajax request by DELETE
PARAMETERS
Name Type Description url
string
URL string
options
object
Options for the Ajax request. Refer to options of ajax().
EXAMPLES
ajax.delete('https://nhn.github.io/tui.code-snippet/v2.3.0');
get(url, options)index.mjsline 541
Send the Ajax request by GET
PARAMETERS
Name Type Description url
string
URL string
options
object
Options for the Ajax request. Refer to options of ajax().
EXAMPLES
ajax.get('https://nhn.github.io/tui.code-snippet/', { params: { version: 'v2.3.0' } });
head(url, options)index.mjsline 541
Send the Ajax request by HEAD
PARAMETERS
Name Type Description url
string
URL string
options
object
Options for the Ajax request. Refer to options of ajax().
EXAMPLES
ajax.options('https://nhn.github.io/tui.code-snippet/v2.3.0');
options(url, options)index.mjsline 541
Send the Ajax request by OPTIONS
PARAMETERS
Name Type Description url
string
URL string
options
object
Options for the Ajax request. Refer to options of ajax().
EXAMPLES
ajax.head('https://nhn.github.io/tui.code-snippet/v2.3.0');
patch(url, options)index.mjsline 541
Send the Ajax request by PATCH
PARAMETERS
Name Type Description url
string
URL string
options
object
Options for the Ajax request. Refer to options of ajax().
EXAMPLES
ajax.patch('https://nhn.github.io/tui.code-snippet/v2.3.0', { beforeRequest: () => showProgressBar(), complete: () => hideProgressBar() });
post(url, options)index.mjsline 541
Send the Ajax request by POST
PARAMETERS
Name Type Description url
string
URL string
options
object
Options for the Ajax request. Refer to options of ajax().
EXAMPLES
ajax.post('https://nhn.github.io/tui.code-snippet/', { contentType: 'application/json', params: { version: 'v2.3.0' } });
put(url, options)index.mjsline 541
Send the Ajax request by PUT
PARAMETERS
Name Type Description url
string
URL string
options
object
Options for the Ajax request. Refer to options of ajax().
EXAMPLES
ajax.put('https://nhn.github.io/tui.code-snippet/v2.3.0', { success: ({status, statusText}) => alert(`success: ${status} ${statusText}`), error: ({status, statusText}) => alert(`error: ${status} ${statusText}`) });