使用 jQuery 验证上传表单,在submitHandler() 中使用ajax...不起作用

发布于 2024-09-29 15:43:11 字数 1375 浏览 1 评论 0原文

我正在使用 jquery 验证表单。我想使用ajax提交表单。当我将ajax调用放入validate的submitHandler()中时,浏览器挂起。这是怎么回事?

当我启用验证方法的调试时收到的错误消息是:

未捕获的异常:[异常... “对WrappedNative进行非法操作 原型对象”ns结果: “0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)” 位置:“JS框架:: http://ajax.googleapis.com/ajax/libs /jquery/1.4.2/jquery.min.js :: f :: 第 132 行”数据:否]

这对我来说太神秘了,无法理解。

一些代码:

$(document).ready(function() {
$("#loginForm").validate({
        debug: true,
        errorLabelContainer: $('div.error'),
        wrapper: 'li',
        rules:  {
            last_name: {
                required: true
            }
        },
        messages: {
            last_name: {
                required: "Please enter your last name."
            }
        },
        submitHandler: function(form){
            $.ajax({
                type: "POST",
                url: "test.php",
                data: form,
                success: function(msg){
                    console.log( "Data Saved: " + msg );
                },
                error: function(msg){
                    console.log( "Error: " + msg);
                }
            });
        }
    });

});

该表单是一个非常普通的表单。通过标准 POST 提交工作正常。此外,验证工作正常..只是使用 ajax 提交部分让我失败。

I am using jquery validate with a form. I want to submit the form using ajax. When I put the ajax call in validate's submitHandler() The browser hangs. What's going on?

The error message I get when I enable the validate method's debug is:

uncaught exception: [Exception...
"Illegal operation on WrappedNative
prototype object" nsresult:
"0x8057000c
(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)"
location: "JS frame ::
http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
:: f :: line 132" data: no]

Which is too arcane for me to make sense of.

Some code:

$(document).ready(function() {
$("#loginForm").validate({
        debug: true,
        errorLabelContainer: $('div.error'),
        wrapper: 'li',
        rules:  {
            last_name: {
                required: true
            }
        },
        messages: {
            last_name: {
                required: "Please enter your last name."
            }
        },
        submitHandler: function(form){
            $.ajax({
                type: "POST",
                url: "test.php",
                data: form,
                success: function(msg){
                    console.log( "Data Saved: " + msg );
                },
                error: function(msg){
                    console.log( "Error: " + msg);
                }
            });
        }
    });

});

The form is a very vanilla form. Submitting via a standard POST works fine. Also, the validation works fine... it's just the submitting with ajax part that fails me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒人说梦 2024-10-06 15:43:11

您可能希望在发送之前序列化该表单 - 您可能不想发送 DOM 对象

编辑 RE:comment - 仅选择一些输入并序列化 -

$(form).find(":input[name=inp1] :input[name=inp2]").serialize()

You'll probably want to serialise that form before sending it - you probably don't want to send a DOM object

edit RE:comment - to select only some inputs and serialize -

$(form).find(":input[name=inp1] :input[name=inp2]").serialize()
往昔成烟 2024-10-06 15:43:11

使用 php 函数调用 ajax 方法

<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>

class Ajax{
public function submitForm($data=array()){
    //form - name attribute
    //do   - ajax page do_
    // get - ajax result
    return '<script language="javascript" type="text/javascript">
                $(document).ready(function (data){
                    $(\'form[name='.$data['form'].']\').validate({
                            submitHandler: function(form) {
                                $.ajax({
                                    method : \'post\',
                                    url    : \'' . AJAX_PATH . ''. $data['do'].'\',
                                    data   : $(this).serialize(),
                                    dataType : \'html\',
                                    success : function(data){
                                        $(\'form[name='.$data['form'].']\').slideUp(1000);
                                        $(\''. $data['get'] .'\').html(data).fadeIn(1000);
                                        //$(\''. $data['get'] .'\').html(data).fadeIn(1000);
                                    },
                                    error : function(data) {alert(\'missing file\'); }
                                });
                                return false;
                             }
                    });
                });
            </script>';
}

}

$ajax = new Ajax();

<?php echo $ajax->submitForm(array('do'=>'do_register.php','form'=>'register','get'=>'#message'));?>

call ajax method with php function

<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>

class Ajax{
public function submitForm($data=array()){
    //form - name attribute
    //do   - ajax page do_
    // get - ajax result
    return '<script language="javascript" type="text/javascript">
                $(document).ready(function (data){
                    $(\'form[name='.$data['form'].']\').validate({
                            submitHandler: function(form) {
                                $.ajax({
                                    method : \'post\',
                                    url    : \'' . AJAX_PATH . ''. $data['do'].'\',
                                    data   : $(this).serialize(),
                                    dataType : \'html\',
                                    success : function(data){
                                        $(\'form[name='.$data['form'].']\').slideUp(1000);
                                        $(\''. $data['get'] .'\').html(data).fadeIn(1000);
                                        //$(\''. $data['get'] .'\').html(data).fadeIn(1000);
                                    },
                                    error : function(data) {alert(\'missing file\'); }
                                });
                                return false;
                             }
                    });
                });
            </script>';
}

}

$ajax = new Ajax();

<?php echo $ajax->submitForm(array('do'=>'do_register.php','form'=>'register','get'=>'#message'));?>
惯饮孤独 2024-10-06 15:43:11

只是为了详细说明 tobyodavies 的答案。您可能希望将提交更改为

        $.ajax({
            type: "POST",
            url: "test.php",
            data: $(form).serialize(),
            success: function(msg){
                console.log( "Data Saved: " + msg );
            },
            error: function(msg){
                console.log( "Error: " + msg);
            }
        });

ajax 函数期望数据选项为字符串(不完全正确,但对于本例来说很好),同时传入表单 dom 对象。

Just to elaborate of tobyodavies answer. You might want to change your submission to

        $.ajax({
            type: "POST",
            url: "test.php",
            data: $(form).serialize(),
            success: function(msg){
                console.log( "Data Saved: " + msg );
            },
            error: function(msg){
                console.log( "Error: " + msg);
            }
        });

The ajax function expects the data option to be a string (not entirely true but for this case it's fine) while your passing in the forms dom object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文