26
votes

I have a problem with jQuery ajax. I have 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?

6
It's really weird. I don't see any reason at all why this code should produce that. Don't you have any other code anywhere else? - Jeremy Thille
Can you edit your question and add your HTML please ? To help us reproduce the error. - Ivan Gabriele
You have page: a as part of the data you are submitting, where a is a jQuery object - what do you expect that to do? - nnnnnn
How many <a>'s are on the page (nested within #pager, that is)? Post your html. #pager sounds like a pagination of sorts. If you, for example, have thousands of pages in your pagination display (Eg. ?page=1, ?page=2, 50, 100, 1000), all referenced with <div id="#pager"><a href="?page=2">...<a href="?page=10000"></div>, then binding the click handler on ~x <a> elements could easily fill the stack. - mferly

6 Answers

68
votes

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.

9
votes

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

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

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

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

This will solve the issue I guess

2
votes

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.

0
votes

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

0
votes

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=103583 The 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.

        // 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.

0
votes

I want to share my experience,

in my case, it was only a wrong parameter name and exactly the same error message : instead of confID, I put the configID and got this error.

 function openNameEditor() {
    var confID = $("#configStatusList").attr("data-id");
    debugger;
    $.ajax({
        url: '@Url.Action("GetModelNameToChange", "Admin")',
        type: "GET",
        dataType: "HTML",
        data: { configID: configID},//Here, instead of confID, I put configID which doesn't exist in the function.
        success: function (response) {
            $("#name-editor").html(response);
        },
        error: function (er) {
            alert(er.error);
        }
    });

}