Ajax.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. export default class Ajax {
  2. constructor(Dcat) {
  3. this.dcat = Dcat;
  4. Dcat.handleAjaxError = this.handleAjaxError.bind(this)
  5. }
  6. handleAjaxError(xhr, text, msg) {
  7. let Dcat = this.dcat;
  8. Dcat.NP.done();
  9. Dcat.loading(false);// 关闭所有loading效果
  10. $('.btn-loading').buttonLoading(false);
  11. var json = xhr.responseJSON || {}, _msg = json.message;
  12. switch (xhr.status) {
  13. case 500:
  14. return Dcat.error(_msg || (Dcat.lang['500'] || 'Server internal error.'));
  15. case 403:
  16. return Dcat.error(_msg || (Dcat.lang['403'] || 'Permission deny!'));
  17. case 401:
  18. if (json.login) {
  19. return location.href = json.login;
  20. }
  21. return Dcat.error(Dcat.lang['401'] || 'Unauthorized.');
  22. case 419:
  23. return Dcat.error(Dcat.lang['419'] || 'Sorry, your page has expired.');
  24. case 422:
  25. if (json.errors) {
  26. try {
  27. var err = [], i;
  28. for (i in json.errors) {
  29. err.push(json.errors[i].join('<br/>'));
  30. }
  31. Dcat.error(err.join('<br/>'));
  32. } catch (e) {}
  33. return;
  34. }
  35. }
  36. Dcat.error(_msg || (xhr.status + ' ' + msg));
  37. }
  38. }