使用 ajax 和 bootbox 的非法调用

Illegal Invocation using ajax and bootbox

大家早上好。

所以我一直在使用 jquery 的 bootbox 插件开发这个系统,以制作一系列不错的 windows 和表单,这些表单可以进行订单处理和管理等等。在这种特殊情况下,我有一个接收功能,可以在单击 link 时获取订单数据,然后构建一个表单并将该表单发回。然后,当我单击提交时,它应该仍会处理,但在这种情况下,我收到 illegal invocation 错误。我已经在 bootbox 调用外部的 ajax 和 success 下的回调内部尝试了这个函数。我两种方式的运气都是零,所以我希望有人能帮我解决这个问题。

下面是我的代码:

$('#new-inventory').on('click', '.receive', function(e){
    e.preventDefault();
    var id = $(this).data('id');
    $.ajax({
        url : 'assets/server/home/receive.php',
        type : 'POST',
        data : {id : id},
        dataType : 'JSON',
        success : function(data){
            bootbox.dialog({
                title : "Receive Tires",
                className : 'receive-tires-window',
                message : "<main class='row'>"+
                    "<section class='col-xs-12'>"+
                        data.message+
                    "</section>"+
                "</main>",
                buttons : {
                    success : {
                        label : 'Receive',
                        className : 'btn-success',
                        callback : function(){

                            e.preventDefault();
                            var formData = new FormData($('#rcvfrm')[0]);
                            $.ajax({ //error received right here
                                url : 'assets/server/home/process_receiving.php',
                                type : 'POST',
                                data : formData,
                                dataType : 'JSON',
                                success : function(data){
                                    if(!data.errors){
                                        bootbox.alert(data.message, function(){
                                            location.reload();
                                        });
                                    }else{
                                        bootbox.alert(data.message);
                                    }
                                }
                            });

                            return false;
                        }
                    },
                    danger : {
                        label : 'Cancel',
                        className : 'btn-danger',
                        callback : function(){

                        }
                    }
                }
            });

            $('.receive-tires-window').on('shown.bs.modal', function(){
                $('.receive-tires-window #recv_date').datepicker();
            });

        }
    });
});

这是因为 jQuery ajax 自动尝试将数据强制转换为字符串,您在不可强制转换的格式数据对象中拥有该字符串。

要解决此问题,您需要将这两个选项添加到第二个请求中:

processData: false,
contentType: false

您可以阅读选项 on the documentation. See also this and this 问题的更详细解释。