基於網絡聊天室的社交遊戲 -- vue、axios
阿新 • • 發佈:2017-06-21
swe clas eth .html prot session 比較 eba n)
前一篇系列博文的傳送門:http://www.cnblogs.com/lastpairs/p/6993237.html
客戶端代碼github地址 https://github.com/xxyjskx1987/lastpairswebapp
服務器端代碼github地址 https://github.com/xxyjskx1987/lastpairsnodeserver
項目演示地址
axios,建議在vue2.0替換resource的開發組件,用於請求資源。
安裝axios
npm install axios -save
代碼中引用
import axios from ‘axios‘ axios.defaults.withCredentials= true; Vue.prototype.$http = axios; Vue.prototype.$apidomain = ‘http://127.0.0.1:3000‘;
這裏面只提出了域名,如果項目比較大可自定義api層,withCredentials屬性是用於在請求中帶上cookie,以實現session功能。
post請求
this.$http({ url: this.$apidomain + ‘/login‘, method: ‘post‘, data: {}, transformRequest: [function (data) { // Do whatever you want to transform the data let ret = ‘‘ for (let it in data) { ret += encodeURIComponent(it) + ‘=‘ + encodeURIComponent(data[it]) + ‘&‘ } return ret }], headers: {‘Content-Type‘: ‘application/x-www-form-urlencoded‘ } }).then(function (res) { console.log(res.data); }).catch(function (error) { console.log(error); });
get請求
this.$http.get(this.$apidomain).then((res)=>{ console.log(res.data); }).catch((err)=>{ console.log(err); });
基於網絡聊天室的社交遊戲 -- vue、axios