6 Replies - 9020 Views - Last Post: 15 August 2014 - 09:49 AM

#1 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Nested Ajax Call inside of Ajax

Posted 14 August 2014 - 12:40 PM

function CreateClone () {
$.post(URL, {websiteid: WebsiteID, recid: $(this).attr('recid'), pricing: $('#ClonePricing').prop('checked'), pres: $('#ClonePresentations').prop('checked'), advdetails: $('#CloneAdvancedDetails').prop('checked'), delivery: $('#CloneDelivery').prop('checked'), feeds: $('#CloneDelivery').prop('checked'), time: $.now() }, function (data) {
var response = $.parseHTML(data); var ErrorMessage = $(response).filter('#ErrorMessage').html(); var ReturnContent = $(response).filter('#ReturnContent').html();
if (typeof ErrorMessage != 'undefined' && ErrorMessage.length > 0) { alert(ErrorMessage); } else { LoadInventoryBySearch(); }
});
}



Whenever I make a call similar to this, the nested call LoadInventoryBySearch(Another AJAX call) is completely ignored. My ajax will literally not even see it. If I placed it inside of setTimeout for at least 100 ms it will call it. Why do I have to do that? I've seen in many places people are nesting ajax requests inside of other ajax requests with no delay. Synchronous is not turned on. Any one have any ideas? I have many calls like this, and it's very very aggravating to have to place setTimeout around all my other requests and properly separate them by time as the error will repeat if they're all setting a timeout for the same amount of time.

Edit: Just want to add that the need for these calls is for updating user data on the screen. If they add or delete information a part of the page needs to refresh.

Also: I have FireBug up, and Chrome Network tools. Both are not showing any other requests outside of the CreateClone function. Like I said completely ignoring it despite all conditions allowing it to do so.

This post has been edited by trevster344: 14 August 2014 - 12:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Nested Ajax Call inside of Ajax

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Nested Ajax Call inside of Ajax

Posted 14 August 2014 - 01:07 PM

You are more likely to get help with some nice indenting:
function CreateClone () {
    $.post(URL, {
        websiteid: WebsiteID,
        recid: $(this).attr('recid'),
        pricing: $('#ClonePricing').prop('checked'),
        pres: $('#ClonePresentations').prop('checked'),
        advdetails: $('#CloneAdvancedDetails').prop('checked'),
        delivery: $('#CloneDelivery').prop('checked'),
        feeds: $('#CloneDelivery').prop('checked'),
        time: $.now()
    },
    function (data) {
        var response = $.parseHTML(data); 
        var ErrorMessage = $(response).filter('#ErrorMessage').html(); 
        var ReturnContent = $(response).filter('#ReturnContent').html();
        if (typeof ErrorMessage != 'undefined' && ErrorMessage.length > 0) {
            alert(ErrorMessage);
        } else {
            LoadInventoryBySearch();
        }
    });
}

Was This Post Helpful? 0
  • +
  • -

#3 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: Nested Ajax Call inside of Ajax

Posted 14 August 2014 - 01:34 PM

Quote

Whenever I make a call similar to this, the nested call LoadInventoryBySearch(Another AJAX call) is completely ignored. My ajax will literally not even see it.

Considering the fact that browsers will not just ignore valid code, what exactly happens when you run it? What do you expect to happen? What is the other function supposed to do? If you replace it with a call to alert, do you see the alert?
Was This Post Helpful? 0
  • +
  • -

#4 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: Nested Ajax Call inside of Ajax

Posted 14 August 2014 - 06:25 PM

The function is passed without being called, as in I expected it to hit the call and either throw an error or process the request but neither firebug or any other tools are catching any errors or requests. I've placed alerts just before that function is called, and at the start of the function LoadInventoryBySearch. The alert before the function call is fired off, but the alert inside the function itself is not. I've observed all developer network tools that I can, and the request will not occur unless it's delayed 100ms+ which is strange given the purpose of AJAX. It's not a complex script at all either, and I've debugged the hell out of it. The script loads in it's own script tag as well and no exceptions are being raised in a try catch. The LoadInventoryBySearch makes a request using the exact same code except it uses a different command, and posts different variables. Indenting is applied where I'm writing it, unfortunately when I copied and pasted my code here it did not come out as pretty.

To sum up how this code would be used:

1. Customer edits data in row
2. Customer presses save
3. Click event posts ajax request
4. If no user defined errors occur, success callback calls second ajax request for new data
5. LoadInventoryBySearch sends ajax request, success callback refreshes the table with new data

This post has been edited by trevster344: 14 August 2014 - 06:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 ge∅   User is offline

  • D.I.C Lover

Reputation: 319
  • View blog
  • Posts: 1,335
  • Joined: 21-November 13

Re: Nested Ajax Call inside of Ajax

Posted 15 August 2014 - 01:39 AM

jQuery often fails silently, it's by design.

What happens when you execute LoadInventoryBySearch outside of the ajax callback ?

Why do you do nested ajax requests in the first place ? Sounds weird.
Was This Post Helpful? 0
  • +
  • -

#6 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: Nested Ajax Call inside of Ajax

Posted 15 August 2014 - 07:01 AM

I've come to that conclusion a few times, just strange that I'd be the first to make a note of it. That callback works just fine, it reloads the user data as it should. The nested ajax request is really just in response to data being updated giving the user a seamless experience like they would on their own desktop. I'm now going to try the $.when function of Jquery.

To be honest though, ajax requests were created for this sort of thing. Updating sections of a page.

This post has been edited by trevster344: 15 August 2014 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: Nested Ajax Call inside of Ajax

Posted 15 August 2014 - 09:49 AM

Another suggestion I would have to help debug the problem is to use breakpoints to step through the code and see what it's actually doing. It's possible that, for example, an exception is being thrown but is being caught and silently discarded.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1