简体   繁体   English

jQuery ajax “递归过多”

[英]jQuery ajax “too much recursion”

I have an procedure that runs throught 5 consecutive steps, and in my page, I'm calling an ajax method for each of them.我有一个运行 5 个连续步骤的过程,在我的页面中,我为每个步骤调用 ajax 方法。

The idea is to run the first, if all is ok the second, and so on.想法是运行第一个,如果一切正常,则运行第二个,依此类推。

My code is:我的代码是:

$("#foo").click(function(){
    $.ajax({
        url: 'ajax.php',
        async: false,
        data: {
            step: 1
        },
        dataType: 'json',
        type: 'GET',
        success: walk_through(data)
    });
});

function walk_through(data)
{
    if(data.status == 'ok')
    {
        if(data.next_step == 'end')
        {
            // All steps completed
        }
        else
        {
            // Current step completed, run next one
            $.ajax({
                url: 'ajax.php',
                async: false,
                data: {
                    step: data.next_step
                },
                dataType: 'json',
                type: 'GET',
                success: walk_through(data)
            });
        }
    }
    else
    {
        alert("Error.");
        console.log(data);
    }
}

Im getting "too much recursion" error, even if my ajax calls are set as syncronous.. why?我收到“递归过多”错误,即使我的 ajax 调用设置为同步 .. 为什么?

Change改变

success: walk_through(data)

To

success: walk_through

You want the function walk_through to be the success handler, but you don't want to call the function immediately.您希望 function walk_through成为成功处理程序,但您不想立即调用 function。

Your JavaScript is wrong on your AJAX call:您的 JavaScript 在您的 AJAX 调用中是错误的:

$.ajax({
    url: 'ajax.php',
    async: false,
    data: {
      step: data.next_step
    },
    dataType: 'json',
    type: 'GET',
    success: walk_through //You had walk_through(data), which makes a function call, rather than a reference to a function.
});

Your walk_through function is calling itself everytime it is succesful.您的演练 function 每次成功时都会调用自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM