Ajax.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. var json = xhr.responseJSON || {}, _msg = json.message;
  11. switch (xhr.status) {
  12. case 500:
  13. return Dcat.error(_msg || (Dcat.lang['500'] || 'Server internal error.'));
  14. case 403:
  15. return Dcat.error(_msg || (Dcat.lang['403'] || 'Permission deny!'));
  16. case 401:
  17. if (json.login) {
  18. return location.href = json.login;
  19. }
  20. return Dcat.error(Dcat.lang['401'] || 'Unauthorized.');
  21. case 419:
  22. return Dcat.error(Dcat.lang['419'] || 'Sorry, your page has expired.');
  23. case 422:
  24. if (json.errors) {
  25. try {
  26. var err = [], i;
  27. for (i in json.errors) {
  28. err.push(json.errors[i].join('<br/>'));
  29. }
  30. Dcat.error(err.join('<br/>'));
  31. } catch (e) {}
  32. return;
  33. }
  34. }
  35. Dcat.error(_msg || (xhr.status + ' ' + msg));
  36. }
  37. }