1. 程式人生 > >20180204.ajax提交註冊資料;axios跨域請求得到json字串;實現註冊功能

20180204.ajax提交註冊資料;axios跨域請求得到json字串;實現註冊功能

我設定本地阿帕奇後端伺服器埠是8080,前端埠是8081,注意區別
首先要給註冊彈窗一個方法,核實兩次密碼是否一致,在modal1元件中新增程式碼
verify:function(){

        if(this.pass==this.pass1){

//下面是ajax部分,提交使用者寫的name和密碼,name和密碼請提前雙向繫結,否則永遠沒結果。

            axios.post('http://localhost:8080/UserManager/adduser?name='+this.name+'&password='+this.pass)
            .then(function(response){
              console.log(response);
            })
            .catch(function(err){
              console.log(err);
            });

//註冊完了要關閉該視窗

            this.$emit('close');
        }
    },

然後會發現從後端javaweb得到的json字串顯示不出來,提示錯誤,需要跨域請求才能得到,所以要找到專案根目錄下的config/index。js檔案。找到proxytable,新增程式碼
proxyTable: {

    '/api': {
    target: 'http://localhost:8080',
    changeOrigin: true,
    pathRewrite: {
      '^/api': ''
    }
  }
    },

接下來回到modal1元件,ajax部分引數程式碼需要修改為
axios.post('/api/UserManager/adduser?name='+this.name+'&password='+this.pass)
,別的不變。
瀏覽器獲取後端的json字串成功。