Ajax

Ajax Service

Purpose

The ajax function performs HTTP requests (GET, POST, PUT, PATCH, DELETE) and handles responses or errors.

Parameters

  • url (String): The URL to which the request is made.
  • options (RequestOptions | null): An optional configuration object for the request. If not provided, default options are used.

Request Option Types

ActionDescription
paramsData sent with the request (for POST, PUT, etc.). Default is null.
methodHTTP method to use ('get'
queryQuery parameters for GET requests. Default is null.
headersCustom headers for the request. Default is null.
showNotificationsFlag to show notifications. Default is true.
showLoadingIndicatorFlag to show loading indicator. Default is true.
severitySeverity of the error for logging purposes. Default is null.
debugFlag to enable debug mode. Default is false.
timeoutTimeout for the request in milliseconds. Default is 30000 (30 seconds).

Behavior

  • GET Requests: Appends query parameters to the URL.
  • POST/PUT/PATCH/DELETE Requests: Sends params as the body of the request.
  • Default request headers are set to {'accept': 'application/json'} if no custom headers are provided.

Error Handling

  • If the request fails, the errorHandler function is called with the error details.
  • Logs and handles errors based on the severity and additional details.

Responses

  • On success: Returns an object with success: true and the data received.
  • On failure: Returns an object with success: false, error details, and error data (if available).

Default Values

  • If no options are provided, defaults are set (e.g., GET method, 30-second timeout, notifications enabled).
const response = await ajax('/api/data', {
    method: 'get',
    query: { key: 'value' },
    showNotifications: true
});

Copyright © 2026