4

I have an procedure that runs throught 5 consecutive steps, and in my page, I'm calling an ajax method for each of them.

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?

3
  • Does the code work despite the error, or is it failing? Do you really want the requests to be synchronous? It wouldn't be recursive at all if they were asynchronous. Commented Jun 13, 2011 at 12:41
  • you obviously do not ever get next_step == 'end' check what is the response Commented Jun 13, 2011 at 12:44
  • @Pointy: no, I dont need the requests to be syncronous, but i thought the problem was that Commented Jun 13, 2011 at 12:57

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 Good spot. This is the actual problem. It will call the method to get the value to put in the success property, so it will never get as far as doing any ajax call at all.
This for sure. By passing walk_through(data) you're executing function and passing result as onSuccess method. You need to pass method itself.
Thats was the problem! Thanks!
2

Your JavaScript is wrong on your AJAX call:

$.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.
});

Comments

0

Your walk_through function is calling itself everytime it is succesful.

1 Comment

Actually it calls itself even when it's not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.