vue json传参网络请求

vue.confog.js

module.exports = {
   lintOnSave: false,
   outputDir: "dist",
   assetsDir: "static",
   productionSourceMap: false,
   publicPath: "./",
   devServer: {
     open: true,
     host: "0.0.0.0",
     port: 8080,
     hotOnly: false,
     proxy: {
       api: {
         //服务器端接口地址
         // http://192.168.10.107:8080/yzl/user/jsontype/2
         // target: "https://elm.cangdu.org/",
         target: "http://192.168.10.109:8080",
         //
         ws: true,
         //是否跨域
         changeOrigin: true,
         pathRewrite: {
           "^/api": "/",
         },
       },
     },
   },
 };
 

2http-service

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 {
    // http://192.168.10.109:8080/yzl/user/adduser
    // {"userName":"金贵江","password":"123c","phone":"159888","age":"23","email":"345","sex":"男"}
    // http://192.168.10.109:8080/yzl/user/queryuser
    // {"userName":"金贵江","password":"123a"}
    PostQueryuser (datas) {
        // let datas = {userName:userName,password:password};
        return   axios({
            headers: {
              "Content-Type": "application/json;",
            },
            method: "post",
             dataType: 'jsonp',
            transformRequest: [
              function(data) {
                // 对 data 进行任意转换处理
                console.log(data);
                return JSON.stringify(datas);
              },
            ],
            url: "api/yzl/user/queryuser",
            params: datas,
          })
    },
    PostAdduser () {
        return  axios({
            url: 'api/yzl/user/adduser',
            method: 'post',
            // params: wxuser
          })
    },
    repairSign() {
        return  axios({
            url: 'api/v2/login',
            method: 'post',
            // params: wxuser
          })
    },
    //登陆
    postRegistered() {
        return axios.post('api/v2/login')
    },
    //首页数据加载
    getHome() {
        return axios.get('api/show/')
    },
    //数据详情页

    getDetail(id) {
        return axios.get('api/show/details/', { params: { id } });
    },
    //最新专题
    getTopic() {
        return axios.get('api/v1/web/index_card')
    },
    //详情
    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 } })
    }
}

3

   get() {
      let datas = {userName:"金贵江",password:"123a"};
      axios({
        headers: {
          "Content-Type": "application/json;",
        },
        method: "post",
         dataType: 'jsonp',
        transformRequest: [
          function(data) {
            // 对 data 进行任意转换处理
            console.log(data);
            return JSON.stringify(datas);
          },
        ],
        url: "api/yzl/user/queryuser",
        params: datas,
      })
        .then(function(res) {a
       console.log(res);
        })
        .catch(function(error) {a
          console.log(error);
        });
    },
v-model="signin.userName"
 
<button @click="PostQueryuser(signin)">登录</button>

 data() {
    return {
      signin: { userName: "金贵江", password: "123c" },
}}

    PostQueryuser() {
      const requestParameters = Object.assign({}, this.signin);
      http.PostQueryuser(requestParameters).then((res) => {
        requestParameters.userName = this.signin.userName;
        requestParameters.passwor = this.signin.passwor;
        if (res.message === "用户不存在") {
          this.$toast.fail("账号不存在");
        } else if (res.message === "success") {
          this.$toast.fail("登录成功");
          this.$router.push({
            path: "/EnterSystem",
            name: "EnterSystem",
          });
        }
      });
    },

 

posted @ 2021-08-13 13:27  zjxgdq  阅读(921)  评论(0编辑  收藏  举报