Javascript jQuery ajax 发布未捕获的 RangeError:超出最大调用堆栈大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37883313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:51:01  来源:igfitidea点击:

jQuery ajax post Uncaught RangeError: Maximum call stack size exceeded

javascriptjquery

提问by user3609841

I have a problem with jQuery ajax. I have javascript

我有 jQuery ajax 的问题。我有 JavaScript

 <script type="text/javascript">
    $(function() {
        $('body').on("click", "#pager a", function(e) {
            e.stopPropagation();
            e.preventDefault();
            var a = $(this);
            var model = $('#searchForm').serialize();
            $.ajax({
                url: '/Product/Products',
                type: 'POST',
                data: {
                    model: model, page: a
                },
                success: function (data) {
                    alert('success');
                    $('#productsList').html(data);
                }
            });
        });
    });
</script>

This code produce error "Uncaught RangeError: Maximum call stack size exceeded" and I don't understand why. I have no trigger, I used preventDefault and stopPropagation, but I still have this error. Can anyone help me?

此代码产生错误“Uncaught RangeError: Maximum call stack size exceeded”,我不明白为什么。我没有触发器,我使用了 preventDefault 和 stopPropagation,但我仍然有这个错误。谁能帮我?

回答by Gaurav Tyagi

This error can also come if you are passing something in data which is not defined in that scope. Another reason is passing in data with val() directly.

如果您在数据中传递未在该范围内定义的内容,也会出现此错误。另一个原因是直接使用 val() 传递数据。

回答by Akhil Menon

Instead of using var a = $(this) to get the page, use one hidden field and give page value to the field.

不要使用 var a = $(this) 来获取页面,而是使用一个隐藏字段并为该字段提供页面值。

<input type="hidden" value="xyzpage" id="pageValue">

var pageVal = $("#pageValue").val();

data: {
         model: model, page:pageVal 
      },

This will solve the issue I guess

这将解决我猜的问题

回答by Ivan Gabriele

You need to take off the var a = $(this);. I don't know what you try to achieve there but using a the jQuery wrapped clicked element as request data is a non-sense.

你需要脱掉var a = $(this);. 我不知道你想在那里实现什么,但是使用 jQuery 包装的 clicked 元素作为请求数据是没有意义的。

回答by qwerty

Endless loop can also cause this kind of error. View that you don't call same function inside function.

无限循环也会导致这种错误。查看您没有在函数内部调用相同的函数。

回答by K.Serg

I ran into such a problem when parsing a large piece of JSON using jquery.tmpl.js. This error appears when handling large arrays with the concat() function. Here is a link to the problem: https://bugs.chromium.org/p/chromium/issues/detail?id=103583The problem has not been solved since 2011. To solve it, I had to edit the jquery-3.3.1.js javascript library file. For those who want to repeat this decision, do the following: find the following line in the library file return concat.apply ([], ret);and replace it with the code below.

我在使用 jquery.tmpl.js 解析一大段 JSON 时遇到了这样的问题。使用 concat() 函数处理大型数组时会出现此错误。这里是问题的链接:https: //bugs.chromium.org/p/chromium/issues/detail?id=103583 自2011年以来问题一直没有解决。为了解决它,我不得不编辑jquery-3.3 .1.js javascript 库文件。对于那些想要重复此决定的人,请执行以下操作:在库文件中找到以下行return concat.apply ([], ret);并将其替换为下面的代码。

        // Flatten any nested arrays
        if ([].flat) return ret.flat();


        var ret2 = [];

        ret.forEach(function (i) {

            if (i instanceof Array) {

                i.forEach(function (i2) {
                    ret2.push(i2);
                });

            } else {
                ret2.push(i);
            }

        });

        return ret2;

        // original code:
        // return concat.apply([], ret);
        // chrome bug: https://bugs.chromium.org/p/chromium/issues/detail?id=103583

We check if there is a flat() function in the browser's arsenal, for example, it has a chrome browser, and if there is - simply merge the data arrays - nothing more is needed. If not, the browser will go a slower path, but at least there will be no error.

我们检查浏览器的武器库中是否有 flat() 函数,例如,它有一个 chrome 浏览器,如果有 - 只需合并数据数组 - 就不需要更多了。如果没有,浏览器会走更慢的路径,但至少不会出错。