支付宝赞助帐号:service@kuitao8.com 

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

Created2017-05-31   Views 62    Author 懒人程序

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

    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")));
上一篇: node.js写入文件和创建目录的方法
下一篇: vue.js 接收ajax请求插件vue-resource
支持键盘 ← →

邮件订阅

订阅我们的精彩内容