common.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const getRootPath = () => {
  2. const protocol = window.location.protocol;
  3. const host = window.location.host;
  4. const localhostPath = protocol + '//' + host;
  5. return 'http://192.168.1.110:8083';
  6. // return localhostPath;
  7. }
  8. // 获取API的地址
  9. const getApiURL = (apiName) => {
  10. return getRootPath() + apiName;
  11. }
  12. const ajaxGet = (url, data) => {
  13. let requestUrl = url
  14. let paramCount = 0;
  15. if (data) {
  16. data.forEach(item => {
  17. requestUrl += (paramCount > 0) ? '&' : '?';
  18. requestUrl += (`${item[0]}` + `=${item[1]}`);
  19. paramCount++;
  20. })
  21. }
  22. return new Promise((resolve, reject) => {
  23. $.ajax({
  24. url: getApiURL(requestUrl),
  25. type: 'get',
  26. contentType: 'application/json',
  27. success: function (data) {
  28. resolve(data);
  29. },
  30. error: function (XMLHttpRequest, textStatus, errorThrown) {
  31. reject(XMLHttpRequest);
  32. },
  33. });
  34. });
  35. }
  36. const ajaxPost = (url, params) => {
  37. return new Promise((resolve, reject) => {
  38. $.ajax({
  39. url: getApiURL(url),
  40. type: 'post',
  41. contentType: 'application/json',
  42. data: JSON.stringify(params),
  43. success: function (data) {
  44. resolve(data);
  45. },
  46. error: function (XMLHttpRequest, textStatus, errorThrown) {
  47. reject(XMLHttpRequest);
  48. },
  49. });
  50. });
  51. }