最新消息: 新版网站上线了!!!

webpack vue2.0 饿了么表单提交解决办法

找了半天原来是在这样解决的

    let para = Object.assign({}, this.ruleForm2);
                        console.log(para);
                        this.$http.post('http://127.0.0.1/api/index.php', para)
                            .then((response) => {
                               console.log(response);
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }

完整代码如下

<script>
    export default {
        data() {
            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('年龄不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value < 18) {
                            callback(new Error('必须年满18岁'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm2.checkPass !== '') {
                        this.$refs.ruleForm2.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm2.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm2: {
                    pass: '',
                    checkPass: '',
                    age: ''
                },
                rules2: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    age: [
                        { validator: checkAge, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        //这个很重要
                        let para = Object.assign({}, this.ruleForm2);
                        console.log(para);
                        this.$http.post('http://127.0.0.1/api/index.php', para)
                            .then((response) => {
                               console.log(response);
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
&lt;/script&gt;

php代码

<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE');
echo json_encode(array('status'=>1,'data'=>file_get_contents("php://input")));
.....

转载请注明:谷谷点程序 » webpack vue2.0 饿了么表单提交解决办法