vue axios发送请求
baseData.js
if (location.href.includes("baidu")) {
apiBaseUrl = 'https://www.baidu.com';
} else {
apiBaseUrl = 'https://baidutest.com';
}
export const baseData = {
apiBaseUrl: apiBaseUrl,
editArticleUrl: editArticleUrl,
};
http.js
import axios from "axios";
import { baseData } from "./baseData";
let httpService = axios.create({
baseURL: baseData.apiBaseUrl,
timeout: 20000,
});
httpService.defaults.headers.common["Content-Type"] =
"application/x-www-form-urlencoded";
export default httpService;
util.js
function getParams2(params) {
let p
if(location.href.includes('192.168')) {
p = {...params, from_mims: 384736}
}else {
p = params
}
//参数转化
let form = new FormData();
Object.keys(p).forEach((key) => {
form.append(key, p[key]);
});
return form;
}
store/index.js
import Vue from "vue";
import Vuex from "vuex";
import http from "@/utils/http";
import { getParams2 } from "@/utils/utils";
Vue.use(Vuex);
export default new Vuex.Store({
state: {},
getters: {},
mutations: {},
actions: {
getHotWord({ commit }, data) {
http.post("/pc/creator/hotword", getParams2(data.params)).then((res) => {
data.cb(res.data);
}).catch((err) => {console.error(err, commit);});
},
}
}
在vue组件里使用:index.vue
methods: {
...mapActions(["getHotWord"]),
hotWordGet() {
this.getHotWord({
params: {},
cb: (res) => {
if (res.return_code == 200) {
this.hotSearchWords = res.return_data;
} else {
///错误处理
}
},
});
}
}
还没有评论,来说两句吧...