locked
Nested $.getJSON() not working RRS feed

  • Question

  • User381809404 posted

    Hi,

    Please look at following code, i am unable to execute nested $.getJSON().  My url's are RESTful and when i run them seperatly in fiddler then they are working perfectly. I have also take care of CORS in the methods. 

    var uri = "http://www.XXXXXX.XXX/WebService/api/controllername/Chk_mehtod/123";
    
    $.getJSON(uri)
    .done(function (data) {
       if (jQuery.isEmptyObject(data))
       {
          alert("data empty");
       }
       else
       {
          var Menu_uri = "http://www.XXXXXX.XXX/WebService/api/controllername/Menu_mehtod/465";
          
    alert("Till this alert, code is working");
    $.getJSON(Menu_uri, function (menudata) { $.each(menudata, function (index, item) { alert("Hyperlink:" + item.Hyperlink); }); }) .done(function () { alert("Inner Success"); }) .fail(function () { alert("Inner Error"); }); } }) .fail(function () { alert("Outer Error"); });

    Thank you.

    Saturday, June 11, 2016 2:23 PM

Answers

  • User381809404 posted

    Hi,

    Now i understand, why it was not working for me because $.getJSON() is asynchronous and i have redirect code in first request... 

    $.getJSON() is a shorthand notation for $.ajax() which can be configured to be synchronous.

    I learned too many new things while solving this bug. 

    Thank you for response.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 13, 2016 6:45 PM

All replies

  • User61956409 posted

    Hi Pritesh Mehta,

    Please debug your code to check the return data on .done() callback function of the first AJAX request and make sure whether the code in else condition could fire when jQuery.isEmptyObject(data) is false. And please make sure whether the second AJAX request could be sent.

    Best Regards,

    Fei Han

    Monday, June 13, 2016 3:08 AM
  • User381809404 posted

    Hi Fei Han,

    Thank you for reply...

    Yes. the first callback function returns the data... also if jQuery.isEmptyObject(data) is false then too i am going into else part till this [ alert("Till this alert, code is working"); ] but not able to call the second AJAX request.

    I have checked it in jsfiddler and it is working fine... but when i run it on android emulator then first getJson working perfectly but second failed. i have doubt that it has problem with the whitelist plugin... because when i run them seperatly, they are working... when i call them in nested then it is not working...

    Monday, June 13, 2016 6:02 AM
  • User381809404 posted

    Hi,

    Now i understand, why it was not working for me because $.getJSON() is asynchronous and i have redirect code in first request... 

    $.getJSON() is a shorthand notation for $.ajax() which can be configured to be synchronous.

    I learned too many new things while solving this bug. 

    Thank you for response.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 13, 2016 6:45 PM