我的qq 2038373094
前提
axios 能够让vue前端和你的服务器后台可以连接上,前后台可以传输静态的数据
vue实战——vue中发送AJAX请求 https://blog.csdn.net/qq_37591637/article/details/90047745
表单的html代码
<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item label="用户名" prop="username">
<el-input v-model.number="ruleForm.username"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="确认密码" prop="checkPass">
<el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>
页面效果如下
js代码如下
<script>
import Head from '../../components/common/Head'
import axios from 'axios'
import Qs from 'qs'
export default {
components:{
Head
},
data() {
var checkUser = (rule, value, callback) => {
if (!value) {
return callback(new Error('用户名不能为空'));
}
setTimeout(() => {
if (!Number.isInteger(value)) {
callback(new Error('请输入数字值'));
} else {
callback();
// if (value != 11) {
// callback(new Error('手机格式不正确'));
// } else {
// callback();
// }
}
}, 1000);
};
var vapass = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入密码'));
} else {
if (this.ruleForm.checkPass !== '') {
this.$refs.ruleForm.validateField('checkPass');
}
callback();
}
};
var varepass = (rule, value, callback) => {
if (value === '') {
callback(new Error('请再次输入密码'));
} else if (value !== this.ruleForm.password) {
callback(new Error('两次输入密码不一致!'));
} else {
callback();
}
};
return {
ruleForm: {
password: '',
checkPass: '',
username: ''
},
rls: {
password: [
{ validator: vapass, trigger: 'blur' }
],
checkPass: [
{ validator: varepass, trigger: 'blur' }
],
username: [
{ validator: checkUser, trigger: 'blur' }
]
}
};
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
/*在这里进行跨域请求*/
this.axios({
method: "post",
url: '/api/PsychoSys/regedit.action',
data: Qs.stringify(this.ruleForm)
})
.then(function(res) {
console.log(res);
})
.catch(function(err) {
console.log(err);
});
/*在这里进行跨域请求*/
} else {
console.log('error submit!!');
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
}
}
}
</script>
图解关系
注意