目录
axios,他是一个HTTP 库,可以发送Http请求
1、下载配置axios
链接:https://pan.baidu.com/s/1S37MiG365RgYVMfhDFI9ZA
提取码:关注联系
2、代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="./js/vue.min.js"></script>
<script src="./js/axios.min.js"></script>
<style>
label {
width: 60px;
display: inline-block;
text-align: right;
margin-right: 8px;
}
</style>
</head>
<body>
<div id="app">
<input type="button" value="密码登录" @click="isSms=false"/>
<input type="button" value="短信登录" @click="isSms=true"/>
<div v-show="isSms">
<p>
<label>手机号</label>
<input type="text" placeholder="手机号" v-model="sms.mobile">
</p>
<p>
<label>验证码</label>
<input type="text" placeholder="验证码" v-model="sms.code">
</p>
</div>
<div v-show="!isSms">
<p>
<label>用户名</label>
<input type="text" placeholder="用户名" v-model="info.username">
</p>
<p>
<label>密码</label>
<input type="password" placeholder="密码" v-model="info.password">
</p>
</div>
<input type="button" value="登 录" @click="loginForm"/>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
isSms: false,
info: {
username: "",
password: "",
},
sms: {
mobile: "",
code: ""
}
},
methods: {
loginForm: function () {
// 1.获取用户输入的值
let dataDict = this.isSms ? this.sms : this.info;
let url;
if (this.isSms) {
url = "https://api.luffycity.com/api/v1/auth/mobile/login/?loginWay=mobile";
} else {
url = "https://api.luffycity.com/api/v1/auth/password/login/?loginWay=password";
}
// 2.想某个地址发送网络请求 axios
// https://api.luffycity.com/api/v1/auth/password/login/?loginWay=password
// {"username":"alex123","password":"999"}
// https://api.luffycity.com/api/v1/auth/mobile/login/?loginWay=mobile
// {"mobile":"18630087660","code":"123123"}
axios({
method: "post",
url: url,
data: dataDict,
headers: {
"Content-Type": "application/json"
}
}).then(function (res) {
if (res.data.code === -1) {
alert(res.data.msg);
return;
}
// 登录成功后跳转
window.location.href = "https://www.luffycity.com"
}).catch(function (error) {
alert("请求异常,请重新操作。")
})
}
}
})
</script>
</body>
</html>