跨域与网络请求配置

https://www.jianshu.com/p/0eb0fd72cdbc

拼接

http://192.168.0.185:8081/api/common/getHomeData

vue.config.js

module.exports ={
    devServer:{
        proxy:{
            'api':{
                //服务器端接口地址
                target:'https://elm-api.caibowen.net/',
                //
                ws:true,
                //是否跨域
                changeOrigin:true,
                pathRewrite:{
                    '^/api':'/'
                }
            },
        }
    }
}

service

http-service.js

import axios from 'axios';

//拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    if (response.status == 200) {
        return response.data;
    }
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});

export default {
    //登陆
    postRegistered() {
        return axios.post('https://elm.cangdu.org/v2/login')
    },
    //首页数据加载
    getHome() {
        return axios.get('api/show/')
    },
    //数据详情页

    getDetail(id) {
        return axios.get('api/show/details/', { params: { id } });
    },
    //最新专题
    getTopic() {
        return axios.get('api/common/getHomeData')
    },
    //详情
    getTopicDetail(id) {
        return axios.get('/api/Latest/details/', { params: { id } })
    },
    //获取精品专题/咨询攻略头部信息
    getStrategy() {
        return axios.get('/api/strategy/')
    },
    //获取精品专题/咨询攻略头部信息详情
    getStrategyDetail(id){
        return axios.get('/api/strategy/details/', { params: { id } })
    }
}
home
import http from "../services/http-service";
 
 methods: {
  async getTopic() {
      let temp = await http.getTopic();
      console.log(temp);
    },
  },
  mounted(){
this.getTopic()
  },

 

posted @ 2021-03-15 11:26  zjxgdq  阅读(71)  评论(0编辑  收藏  举报