Ajax.js 1.5 KB

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