您的位置:首页 > Web前端 > Vue.js

vue中post请求使用form表单格式发送数据

2019-04-28 13:53 1436 查看

接着上一篇博文讲post请求发送form表达格式的数据(VUE),getSign(“username=” + _this.user.username + “&password=” + _this.user.password)代表的是算签名,下一篇博文讲。

new Vue({
el: '#app',
data: {
user: {},
result: {}
},
// 发送post请求时,不能发送 Content-Type: application/json;charset=UTF-8 这个格式,因为后台过滤器要进行处理签名
headers: {
'Content-Type': 'application/x-www-form-urlencoded',   // 设置请求头为form表单格式
// 'Content-Type': 'application/json;charset=UTF-8'
},
methods: {
login: function () {
var _this = this;
axios({
method: 'post',
url: '/noauth/login' + getSign("username=" + _this.user.username + "&password=" + _this.user.password),
data: {
username: _this.user.username,
password: _this.user.password
},
transformRequest: [function (data) {  // 将{username:111,password:111} 转成 username=111&password=111
var ret = '';
for (var it in data) {
// 如果要发送中文 编码
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
}
return ret.substring(0,ret.length-1)
}],
}).then(function (response) {
_this.result = response.data;
alert(_this.result.message);
localStorage.setItem("sbkjauth",response.headers["sbkjauth"]);
if (_this.result.status == "0201") {
var url = "/html/index.html";
axios({
method:"get",
url:url+getSign(),
headers:{
"sbkjauth":localStorage.getItem("sbkjauth")
},
}).then(function (resp) {
console.info(resp.data);
})
}
}).catch(function (reason) {
console.error(reason)
})
},
created: function () {
console.info("页面尚未加载完成!")
}
});

如果发现什么问题请留言,毕竟代码都是人写的难免会出错。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐