jquery ajax functionality to respond to internal post and load the data accordingly

Gurpreet Singh Randhawa
Gurpreet Singh Randhawa used Ask the Experts™
on
Modifying the JS Code to work one by one and based upon inside ajax post, it should process the activation

Here is the code i have

$.post('/cfc/mycfc.cfc?method=stop', {id: id}, function(data) {
			if(data.status == 1) {
				return $.post('/register.html?action=remove' {id: id, pid: $('body').attr('data-pid')},callback,'json');
				swal('',data.statusmsg,'success'); - i want to get this inside the second post so it should work properly and trying to make use of async as by using ajax so they get called one by one 
				CheckPause(data);
				$('#wrapper').load( "#Pause");
			}else {
				swal('',data.statusmsg,'error');
			}
		},'json')

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
It is not clear what you are asking.

Why are you doing a return on line 3 - that is going to ensure that lines 4-6 never fire?
modified like this:

$.ajax(type:'POST',url:'/cfc/mycfc.cfc?method=Pause', data: {id : id}, async: false, success: function(data) {
			if(data.status == 1) {
				$.post('/register.html?action=Rremove' {id : id, pid : $('body').attr('data-pid')},function(_data) {
					if(_data.status == 1) {
						swal('',data.statusmsg,'success');	
					}
				});
				CheckPause(data);
				$('#wrapper').load( "#Pause");
			}else {
				swal('',data.statusmsg,'error');
			}
		})

Open in new window


they is a change i would like to make,

1. i want to use the error handling on the first one and if it occurs say _data.status = 0, it should never execute the others, same is the case for the data one, and basically enhance the code to better technique

Thanks
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
i want to use the error handling on the first one and if it occurs say _data.status = 0, it should never execute the others,
If data.status == 0 then lines 3-9 won't fire.

// MAKE ASYNCH CALL
// NB:YOU ARE MISSING AN OPENING '{'
// $.ajax(type:
//        ^
// WHY USE $.ajax HERE AND THEN $.post WHY NOT USE THE SAME FOR BOTH?

$.ajax(type:'POST',url:'/cfc/mycfc.cfc?method=Pause', data: {id : id}, async: false, success: function(data) {

  // FIRES WHEN ASYNCH POST COMPLETES
  // CHECK RETURN - IF data.status == 1 PROCEED OTHERWISE JUMP TO else
  if(data.status == 1) {
    // IF WE GET HERE data.status WAS SET TO 1
    // MAKE ANOTHER ASYNCH CALL (WHY USE $.post HERE AND NOT $.AJAX?????
    $.post('/register.html?action=Rremove' {id : id, pid : $('body').attr('data-pid')},function(_data) {
      if(_data.status == 1) {
        swal('',data.statusmsg,'success');  
      }
      else {
        // HANDLE ERROR HERE
      }
    });
    CheckPause(data);
    $('#wrapper').load( "#Pause");
  }else {
    swal('',data.statusmsg,'error');
  }
})

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial