Guest User

Untitled

a guest
Nov 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. export interface AjaxRequest {
  2. url: string;
  3. method?: string;
  4. data?: any;
  5. }
  6.  
  7. let REPORT_ERRORS = true;
  8.  
  9. export function disableErrorReporting(): void {
  10. REPORT_ERRORS = false;
  11. }
  12.  
  13. export function enableErrorReporting(): void {
  14. REPORT_ERRORS = true;
  15. }
  16.  
  17. export function ajax<T>(req: AjaxRequest): Promise<T> {
  18. return new Promise<T>((resolve, reject) => {
  19. $.ajax({
  20. method: req.method || 'POST',
  21. url: req.url,
  22. headers: {
  23. 'X-CSRF-TOKEN': safeGetMetadata('csrf-token'),
  24. 'Accept': 'application/json'
  25. },
  26. data: req.data
  27. })
  28. .done(resolve)
  29. .fail((data) => {
  30. if (REPORT_ERRORS) {
  31. let err = __(`error-status-${data.status}`);
  32. if (data.responseJSON && data.responseJSON.error) {
  33. err = data.responseJSON.error;
  34. }
  35. if (err) {
  36. showNotification("error", __("error-toast-title"), err);
  37. }
  38. }
  39. reject(data);
  40. });
  41. });
  42. }
Add Comment
Please, Sign In to add comment