0

I want to call pages using Ajax, and when one Ajax call completes, then other by click on button .first Ajax call works but after second ajax call not work.

$('document').ready(function(){
    $('.next').click(function(){
        var a=$("#cs_brief").val();
        var dataObj = {cs_brief : a};
        $.get('b.php',dataObj, function(data) {
            $('#division').html(data);
        });                 
    });
});
5
  • Which is the first call? Commented Nov 14, 2012 at 18:00
  • I only see one ajax call Commented Nov 14, 2012 at 18:01
  • 3
    after second ajax call not work - does it means that ajax which should happen on click does not work? If so - can you show relevant html? Commented Nov 14, 2012 at 18:01
  • when first call complete then i will second call on click <a> which class is next. after ajax call read(function) not call because i call after refresh page so how can be done after first ajax call , in second ready(fuction() not call Commented Nov 14, 2012 at 18:25
  • Jquery is not working after ajax call Commented Nov 14, 2012 at 18:37

2 Answers 2

2

If you need to wait for 2 callbacks to complete you may use $.when So for instance:

var promiseA = $.get('a.php');
var promiseB = $.get('b.php');

$.when( promiseA, promiseB).done( function (dataA, dataB){
  $('#division').html(dataB);
});
Sign up to request clarification or add additional context in comments.

Comments

0

This functionality is achieved by using a simple queue in which ajax calls are executed one after another.

For this, setup a simple queue, like the one described in the following answer by Gnarf: Sequencing ajax requests, referenced below:

(function($) {
  var ajaxQueue = $({});
  $.ajaxQueue = function(ajaxOpts) {
    var oldComplete = ajaxOpts.complete;
    ajaxQueue.queue(function(next) {
      ajaxOpts.complete = function() {
        if (oldComplete) oldComplete.apply(this, arguments);
        next();
      };
      $.ajax(ajaxOpts);
    });
  };
})(jQuery);  

and use it like

function executeAjaxCallsUsingQueue(variants) {
  for (var i = 0; i < variants.length; i++) {
    var variant = variants[i];
    $.ajaxQueue({
        url:  '/url_to_process' ,
        type:     'post', 
        data:  variant ,
        dataType: 'json' , 
        success:  function(response) { }
    });
  }
}

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.