2

可能重复:
$.post 抛出“非法调用”</a>

尝试提交 AJAX 请求时出现以下错误:Uncaught TypeError: Illegal invocation。这是代码:http ://pastie.org/private/px6qinlgydv6cgcwrghvxw ,如果我删除 $.ajax() 函数,问题就会消失。

 <script type="text/javascript">
            $(document).ready(function() {
                $('.inquiry-button').on('click', function(e) {
                    if($('.x-box ul li').length > 0) {
                        $('.x-box, .x-box-bottom').fadeOut('slow', function() {
                            $('.inquiry-form, .inquiry-buttons').fadeIn();
                        })
                    }


                })
                $('.edit-inquiry-submit').on('click', function(e){
                    e.preventDefault();
                    $('.inquiry-form, .inquiry-buttons').fadeOut('slow', function() {
                        $('.x-box, .x-box-bottom').fadeIn();
                    })
                })

                $('.inquiry-submit').on('click', function(e) {
                    e.preventDefault();
                    var valid = 1;
                    var name = $('#your_name');
                    var email = $('#your_email');
                    var company = $('#company_name');

                    if (!name.val() && valid == 1) {
                        var valid = 0;
                        name.focus();           
                        alert('Please provide your Name');
                    }
                    if (!email.val() && valid == 1) {
                        var valid = 0;
                        email.focus();
                        alert('Please provide your Email Address');
                    }
                    if (!company.val() && valid == 1) {
                        //   var valid = 0;
                        //  company.focus();
                        //  alert('Please provide your Company Name');
                    }

                    if (valid == 1) {
                        $('#i-form, .inquiry-buttons').fadeOut('', function() {
                            $('.inquiry-thank-you').fadeIn('', function() {
                                $.ajax({
                                    url: 'http://projectsxxxxxdev.com/cooling/wp-admin/admin-ajax.php',
                                    data: {'action' : 'sendProducts', 'name' : name, 'email' : email, 'company' : company},
                                    dataType : 'json',
                                    type: 'POST',
                                    success : function(data){
                                        console.log(data)

                                    }})


                            });
                        });

                    }
                })  


            })


        </script>
4

1 回答 1

11

您正在尝试在数据参数中传递 jQuery 对象,调用.val()以使用它们的值

$.ajax({
    url: 'http://projectsxxxxxdev.com/cooling/wp-admin/admin-ajax.php',
    data: {'action' : 'sendProducts', 'name' : name.val(), 'email' : email.val(), 'company' : company.val()},
     dataType : 'json',
     type: 'POST',
     success : function(data){
         console.log(data)
     }
});
于 2012-11-19T18:20:00.463 回答