ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
vue推荐ajax语法axios插件 [http://www.axios-js.com/docs/](http://www.axios-js.com/docs/) ``` npm install axios ``` ``` created () { this.token = this.$route.query.token // console.log(this.token)// 输出路径传值 if (this.token) { this.tokenlogin() } else { console.log('需要输入登录跳转跳转') } }, methods: { // tokenlogin登录验证 tokenlogin: function () { const axios = require('axios') axios.get('login/tokenlogin?token=' + this.token).then(res => { if (res.data.code !== 1) return this.$message.error(res.data.msg) this.$message.success(res.data.msg) console.log(res.data.token) window.sessionStorage.setItem('token', res.data.token) this.$router.push('/Home') }) }, // 登录验证 login () { console.log(this.loginForm) // console.log(this.loginForm.password) this.$refs.loginFormRef.validate(async valid => { console.log(valid) if (!valid) return const { data: res } = await this.$http.post('login/login', this.loginForm) if (res.code !== 1) return this.$message.error(res.msg) this.$message.success(res.msg) console.log(res) window.sessionStorage.setItem('token', res.token) this.$router.push('/Home') // console.log(window.sessionStorage.getItem('token')) }) } } ``` 不推荐语法 ``` // POST请求 // this.$http({ // type: 'POST', // url: 'http://json.ui-china.cn/dadian/login/login', // method: '', // // 请求体重发送的数据 // data: { // password: this.loginForm.password, // username: this.loginForm.username // } // // 设置请求头 // // headers: { // // 'Content-Type': '123-123' // // } // }).then(function (deta) { // // 请求成功回调 // console.log(deta) // // 请求成功回调 // }, function () { // // 请求失败回调 // }) // $.ajax({ // type: 'post', // url: 'http://json.ui-china.cn/dadian/login/login', // data: this.loginForm, // async: false, // dataType: 'json', // headers: { // 'Content-Type': '123-123' // }, // success: function (res) { // console.log(res) // if (res.code !== 1) { // console.log(0) // } // console.log(1) // } // }) ```