Promise-based AJAX manager using the Fetch API with callbacks, spinner, and timeout support
All available options for Sadrazam.Ajax.request(options)
| Option | Type | Default | Description |
|---|---|---|---|
| route | string | '' | Endpoint URL. Must be a full URL (https://...) or absolute path (/api/...) |
| type | string | 'post' | HTTP method: get, post, put, delete |
| data | object | FormData | {} | Data to send. For GET, appended as query params. For POST, sent as URL-encoded body or FormData. |
| timeout | number | 15000 | Request timeout in milliseconds |
| spinner | boolean | string | false | Show spinner during request: false, 'main', or 'helper' |
| success | function | () => {} | Called on HTTP 2xx with the response object |
| error | function | () => {} | Called on network error, timeout, or HTTP 4xx/5xx |
| beforeStart | function | () => {} | Called right before the request starts |
| afterEnd | function | () => {} | Called after request completes (success or failure), in the finally block |
| complete | function | () => {} | Called when a server response is received (before success/error logic) |
On HTTP 4xx/5xx errors, the response body is checked for a message key. If found, it is displayed via Snackbar.
// Expected error response body (optional) { "message": { "error": ["Product not found."], "warning": ["Stock is low."] } }
If no message key is present, a generic error is shown.
The Token helper reads/updates a CSRF token from a hidden input on the page.
// Read token const token = Sadrazam.Token.get(); // Update token (e.g. after AJAX response) Sadrazam.Token.update(newTokenValue); // HTML — hidden input in page footer <input type="hidden" name="token_common" value="abc123" />
Fetch data from the server — data is appended as query parameters
Send data to create a new resource — data is sent in the request body
Update an existing resource entirely
Delete an existing resource
Network errors, HTTP errors, and timeout scenarios
Observe the execution order: beforeStart → complete → success/error → afterEnd
Ajax.request() returns a Promise — use it with async/await or .then()/.catch()
Pass spinner: 'main' or spinner: 'helper' to show a loading indicator during the request