Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export interface AjaxRequest {
- url: string;
- method?: string;
- data?: any;
- }
- let REPORT_ERRORS = true;
- export function disableErrorReporting(): void {
- REPORT_ERRORS = false;
- }
- export function enableErrorReporting(): void {
- REPORT_ERRORS = true;
- }
- export function ajax<T>(req: AjaxRequest): Promise<T> {
- return new Promise<T>((resolve, reject) => {
- $.ajax({
- method: req.method || 'POST',
- url: req.url,
- headers: {
- 'X-CSRF-TOKEN': safeGetMetadata('csrf-token'),
- 'Accept': 'application/json'
- },
- data: req.data
- })
- .done(resolve)
- .fail((data) => {
- if (REPORT_ERRORS) {
- let err = __(`error-status-${data.status}`);
- if (data.responseJSON && data.responseJSON.error) {
- err = data.responseJSON.error;
- }
- if (err) {
- showNotification("error", __("error-toast-title"), err);
- }
- }
- reject(data);
- });
- });
- }
Add Comment
Please, Sign In to add comment