zoukankan      html  css  js  c++  java
  • vue与后台交互之post用法

    vue解决跨域

    • 安装 axios

      cnpm install axios --save
    • config/index.js

      proxyTable: {
            '/api': {  //使用"/api"来代替"http://f.apiplus.c" 
              target: 'http://127.0.0.1:8000/', //源地址 
              changeOrigin: true, //改变源 
              pathRewrite: { 
                '^/api': '' //路径重写 
                } 
            } 
          },
    • src/main.js

      import axios from 'axios'
      Vue.prototype.axios = axios
    • src/components下新建文件 User.vue

      <template>
        <div>
              <h2>用户注册</h2>
              username: <input type='text' v-model='username'>
              password: <input type='password' v-model='password'>
              <button @click='register'>
                注册    
            </button>
          </div>
      </template>
      
      <script>
          export default{
              name:'user',
              data(){
                  return {
                      username:'',
                      password:''
                  }
              },
              methods:{
                  register:function(){
                      var params = new URLSearchParams();
                      params.append('name',this.username);
                      params.append('password',this.password);
                      this.axios({
                          method:'post',
                          // api 是 http://127.0.0.1:8000 的简写
                          url:'api/app01/add/',
                          data:params
                      }).then(res=>{
                          console.log(res);
                      }).catch(error=>{
                          console.log(error);
                      })
                  }
              }
          }
      </script>
    • router/index.js

      import user from '@/components/User.vue'
      
      export default new Router({
        routes: [
          {
            path: '/',
            name: 'HelloWorld',
            component: HelloWorld
          },
          {
            path:'/user/register',
            name:'user',
            component:user
          }
        ]
      })
  • 相关阅读:
    bzoj2957 -- 线段树
    bzoj2209 [ JSOI2011 ] -- splay
    bzoj3874 [ AHOI2014 ] -- 爬山算法
    bzoj1038 [ ZJOI2008 ] -- 模拟退火+二分
    bzoj2428 [ HAOI2006 ] -- 模拟退火
    bzoj3680 -- 模拟退火
    bzoj4500 -- 差分约束
    bzoj3527 -- FFT
    bzoj1013 [ JSOI2008 ] -- 高斯消元
    使用nginx try_files 指令 管理静态资源
  • 原文地址:https://www.cnblogs.com/sq1995liu/p/12522798.html
Copyright © 2011-2022 走看看