首页 文章

JavaScript RangeError - 使用jQuery.post时超出了最大调用堆栈大小

提问于
浏览
1

当使用jQuery的.post()函数提交我的表单数据时,我得到一个Uncaught RangeError:超出了最大调用堆栈大小 .

我知道这通常意味着递归但我无法看到递归发生的位置 .

我已将post请求放入函数(submitRequest())中,因此我可以从代码中的2个不同点提交数据 . 它最初位于提交事件内部,并且在那时工作得很好 . 一旦我把它移到外面,就会出现错误 .

有任何想法吗?

JavaScript代码(带注释日志,以便您可以看到流程):

$(document).ready(function() {
    var downloadLink = '',
        downloadName = '',
        details,
        detailsSaved = false;

    $('.js--download').click(function(event) {
        var self = $(this);
        event.preventDefault();
        downloadLink = self.data('filePath'); // Store clicked download link
        downloadName = self.closest('.brochure').find('.brochure__name').html().replace('<br>', ' ');
        if (!detailsSaved) {
            $('#brochure-section').addClass('hide');
            $('#capture-section').removeClass('hide');
            $('html, body').animate({
                scrollTop: $("#capture-section").offset().top
            }, 500);
        } else {
            submitRequest();
        }
        return false;
    });

    $(".submit-btn").click(function(event) {
        var antiSpam = $('input[name=url]').val();
        if (antiSpam != "") {
            outputResultText('Error - Please leave the spam prevention field blank', 'error');
            proceed = false;
            event.preventDefault();
            return false;
        }        

        var name = $('input[name=name]').val(),
            company = $('input[name=company]').val(),
            email = $('input[name=email]').val(),
            phone = $('input[name=phone]').val(),
            proceed = true;

        if(name==""){
            $('input[name=name]').addClass("error");
            proceed = false;
        }
        if(phone==""){
            $('input[name=phone]').addClass("error");
            proceed = false;
        }
        if(email==""){
            $('input[name=email]').addClass("error");
            proceed = false;
        }

        if(!proceed) {
            outputResultText('Please check all required fields', 'error');
            event.preventDefault();
            return false;
        }

        event.preventDefault();
        if(proceed) {
            console.log('About to request'); // Logged out
            submitRequest();
        }

        return false;
    });

    //reset previously set border colors and hide all message on .keyup()
    $("input, textarea").keyup(function() {
        $(this).removeClass("error");
        $(".form-result").fadeOut(100);
    });

    function submitRequest () {
        console.log('Start submitRequest'); // Logged out

        if (!detailsSaved) {
            console.log('Details are NOT saved');
            post_data = {
                'name': name,
                'company': company,
                'phone': phone,
                'email': email,
                'brochure': downloadName,
                'brochure_url': downloadLink
            };
            details = post_data;
        } else {
            console.log('Details are saved');
            post_data = details;
            post_data['brochure'] = downloadName;
            post_data['brochure_url'] = downloadLink;
        }
        console.log('Posting data'); // Logged out
        // CRASH: Uncaught RangeError: Maximum call stack size exceeded 

        $.post(bcf_local_args['post_url'], post_data, function(response){ 
            console.log('Response received');
            if(response.type != 'error') {
                if (detailsSaved) {
                    outputAlert("Thank you for your request to receive our <strong>'"+downloadName+"'</strong> brochure.<br>We'll send you a copy soon to <strong>'"+email+"'</strong>, so please check your inbox.<br>Want it sent to a different email? Simply refresh the page and try again.");
                } else {
                    //reset values in all input fields
                    $('#brochure-capture-form input').val('');
                    $('#brochure-capture-form textarea').val('');
                    $('#capture-section').addClass('hide');
                    $('#brochure-section').removeClass('hide');
                    outputAlert("Thank you for your request to receive our <strong>'"+downloadName+"'</strong> brochure.<br>We'll send you a copy soon to <strong>'"+email+"'</strong>, so please check your inbox.");
                }
                if (!detailsSaved) {
                    detailsSaved = true;
                }
                $('html, body').animate({
                    scrollTop: $(".brochure__alert").offset().top
                }, 500);
            } else {
                outputResultText(response.text, response.type); 
            }
        }, 'json');
    }

    function outputResultText (text, status) {
        var output = '';
        if(status == 'error') {
            output = '<div class="error">'+text+'</div>';
        } else {
             output = '<div class="success">'+text+'</div>';
        }
        $(".form-result").hide().html(output).fadeIn(250);
    }

    function outputAlert (text) {
        var output = '<div>'+text+'</div>';
        $('.brochure__alert').hide().removeClass('hide').html(output).slideDown(250);
        setTimeout( function() {
            $('.brochure__alert').slideUp(250);
        }, 6500);
    }

    // function accessStorage(action, dataKey, dataValue) {
    //     if(typeof(Storage) === "undefined") {
    //         // No support for localStorage/sessionStorage.
    //         return false;
    //     } 
    //     if (action == 'store') {
    //         localStorage.setItem(dataKey, dataValue);
    //     } else if (action == 'retrieve') {
    //         return localStorage.getItem(dataKey);
    //     }
    // }
});

1 回答

  • 1

    我不知道你是否已找到解决方案,但我遇到了“同样”的问题 .

    在我的代码中,我有一个函数,我在上传图像后调用,我将图像名称作为参数传递给我的POST数据所需的其他参数 .

    经过一些研究后我发现浏览器在传递参数方面有一些限制所以问题不是AT $ .post,而是在我的函数调用中 .

    我不知道技术术语,但我'过度使用堆栈参数' .

    所以也许你的问题也不是你的$ .post,而是超出堆栈的其他东西 .

    希望这可以帮助 .

    []的

相关问题