qs庫的npm地址:https://www.npmjs.com/package/qs 功能雖然都是序列化。假設我要提交的數據如下 qs.stringify序列化結果如下name=hehe&age=10 而JSON.stringify序列化結果如下:"{"a":"hehe","age":10}" vu ...
qs庫的npm地址:https://www.npmjs.com/package/qs
功能雖然都是序列化。假設我要提交的數據如下
var a = {name:'hehe',age:10};
qs.stringify序列化結果如下name=hehe&age=10
而JSON.stringify序列化結果如下:"{"a":"hehe","age":10}"
vux中使用post提交表單數據:
this.$http.post(this.$sign.config.url.loginUrl,this.$qs.stringify({ "phone":this.phoneNumber, "vCode":this.loginCode, "smsCode":this.phoneCode }) ) .then(response=>{ console.log(response.data); if(response.data.httpCode == 200){ }else{ } })
在firebug中可以看到傳遞的參數:phone=15210275239&vCode=8vsd&smsCode=1534
在vue中使用axios:
this.$axios.post(loginUrl, { "email": this.email, "password": this.password }, { transformRequest: (data) => { return this.$qs.stringify(data) }, }).then(res => { if(res.data.resultCode == RESULT_CODE_SUCCESS){ console.log('登錄成功'); this.$router.push({name:"home"}) }else{ console.log('登錄失敗'); } }).catch(err => { console.log('登登錄出現錯誤'); })