1

I am trying to perform a nested AJAX call using the following code. The nested call doesn't seem to work. Am I doing anything wrong?

$.ajax({
type: 'GET',
url: "/public/customcontroller/dosomething",
cache: false,
dataType: "html",
success: function(html_input)
{
    $.ajax({
        type: 'GET',
        url: "/public/customcontroller/getjobstatus",
        cache: false,
        dataType: "html",
        success: function(html_input){
        alert(html_input);
        }
    });                                                                       
}
});
4
  • Depends, is the first call successful? Otherwise the function in success is never called. Commented Mar 30, 2010 at 19:58
  • I would call html_input something different in the inner function Commented Mar 30, 2010 at 19:59
  • The first call is successful. I renamed html_input to html_response in the inner function.. Still no luck.. Commented Mar 30, 2010 at 20:04
  • Just curious, why don't you have /public/customcontroller/dosomething return the data you're after? Commented Mar 30, 2010 at 20:16

3 Answers 3

5

You could be having thread-type issues, given that the outside call is asynchronous and may go out of scope before the success handler can come into play. Try breaking the success call out into a separate function.

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

1 Comment

Would love to see an example.
3

Try this

$.ajax({
type: 'GET',
url: "/public/customcontroller/dosomething",
cache: false,
dataType: "html",
async: false,
success: function(html_input)
{
    $.ajax({
        type: 'GET',
        url: "/public/customcontroller/getjobstatus",
        cache: false,
        dataType: "html",
        success: function(html_input){
        alert(html_input);
        }
    });                                                                       
}
});

2 Comments

:In the above code instead of "dataType" you should use "datatype".
@suraj api.jquery.com/jQuery.ajax/#jQuery-ajax-settings states that the option is named dataType.
0

Use the async/await syntax.

async function foo () {
    var html_input = await $.ajax({
        type: 'GET',
        url: "/public/customcontroller/dosomething",
        cache: false,
        dataType: "html"
    })

    var html_input2 = await $.ajax({
        type: 'GET',
        url: "/public/customcontroller/getjobstatus",
        cache: false,
        dataType: "html"
    })

    alert(html_input2)
}
foo().catch(e => console.log('some error:', e))

Comments

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.