request.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var headers = {
  2. 'Content-Type': 'application/x-www-form-urlencoded',
  3. 'X-Requested-With': 'XMLHttpRequest',
  4. };
  5. var requestApi = function (url, data, type) {
  6. return new Promise(function (resolve, reject) {
  7. $.ajax({
  8. headers: headers,
  9. url: url,
  10. data: data,
  11. type: type || "post",
  12. dataType: 'json',
  13. success: function (rem) {
  14. if (rem.code == 200 || rem.status == 200)
  15. resolve(rem);
  16. else
  17. reject(rem);
  18. },
  19. error: function (err) {
  20. reject(err);
  21. }
  22. })
  23. });
  24. }
  25. window.requestGet = function (url, data) {
  26. return requestApi(url, data, 'get');
  27. }
  28. window.requestGet = function (url, data) {
  29. return requestApi(url, data, 'post');
  30. }
  31. window.getUrl = function (opt) {
  32. var m = opt.m || window.module, c = opt.c || window.controlle, a = opt.a || 'index', q = opt.q || '',
  33. p = opt.p || {}, params = '',gets='';
  34. params = Object.keys(p).map(function (key) {
  35. return key + '/' + p[key];
  36. }).join('/');
  37. gets = Object.keys(q).map(function (key) {
  38. return key+'='+ q[key];
  39. }).join('&');
  40. return '/' + m + '/' + c + '/' + a + (params == '' ? '' : '/' + params) + (gets == '' ? '' : '?' + gets);
  41. }