http.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import axios from 'axios';
  2. import Qs from 'qs';
  3. // 请求超时时间
  4. axios.defaults.timeout = 1000000;
  5. axios.defaults.baseURL = 'http://119.3.84.55:8085';
  6. // post 请求头
  7. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
  8. //请求拦截器
  9. axios.interceptors.request.use(
  10. config => {
  11. config.headers = {
  12. 'Content-Type' : 'application/x-www-form-urlencoded',
  13. }
  14. config.headers['Content-Type'] = "application/x-www-form-urlencoded";
  15. return config
  16. },
  17. error => {
  18. return Promise.error(error);
  19. }
  20. );
  21. // 相应拦截器
  22. axios.interceptors.response.use(
  23. response => {
  24. if(response.status === 200) {
  25. return Promise.resolve(response)
  26. } else {
  27. return Promise.reject(response)
  28. }
  29. },
  30. error => {
  31. return Promise.error(error);
  32. }
  33. );
  34. // get 方法
  35. export function get(url, params) {
  36. return new Promise((resolve, reject) => {
  37. axios.get(url, {params: params})
  38. .then(res => {
  39. resolve(res.data);
  40. })
  41. .catch(err => {
  42. reject(err.data);
  43. })
  44. })
  45. }
  46. // post 方法
  47. export function post(url, params) {
  48. return new Promise((resolve, reject) => {
  49. axios.post(url, Qs.stringify(params))
  50. .then(res => {
  51. resolve(res.data);
  52. })
  53. .catch(err => {
  54. reject(err.data);
  55. })
  56. })
  57. }
  58. export default {
  59. get,
  60. post
  61. }