0

i want to check whether is a valid item or not before saving the values. then i create java-script function to check validation and return result. but the problem is this function returns before validate items, the always true the condition above if condition. my code is below. could anyone help me please? this is series of ajax call and i'm not aware of how to use callback for this..

if(IsValidItems() != ''){
//Do something
}

function IsValidItems() {

var IsvalidStatus = '';
var lineqty = 0;
var LineNumber = -1;

var allRowData = jQuery("#tblJQGrid").jqGrid("getRowData");

 for (var i = 0; i < allRowData.length - 1; i++) {
  if (allRowData[i].BulkItem != "False") {
   if (allRowData[i].quantity != '') {
    lineqty = parseInt(allRowData[i].quantity);
    LineNumber = i + 1;
    var postURL = "/BookingDetail/GetItemAvailablity?ItemCode=" + allRowData[i].itemCode + "&StartDate=" + allRowData[i].StartDate + "&EndDate=" + allRowData[i].EndDate + "&srno=" + allRowData[i].srno + "&locationID=" + allRowData[i].Location;                    
    $.ajax({
     url: postURL,
     dataType: "json",
     contentType: "application/json; charset=utf-8",
     data: "",
     type: "POST",
     async: true,
     dataFilter: function (data) {
      return data;
     },
     success: function (result) {
      if (lineqty > parseInt(result)) {
       IsvalidStatus = IsvalidStatus + "," + LineNumber;
      }
     },
     error: function (XMLHttpRequest, textStatus, errorThrown) { }
    });
   }
  }
 }
 return IsvalidStatus;
}
          
Pradeep Asanka
  • 403
  • 1
  • 5
  • 11
  • Possible duplicate of [jQuery: Return data after ajax call success](http://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success) – gyre Mar 15 '17 at 23:58
  • No actually this is series of ajax call and my actual code longer and complicated than this. i'm not aware that how to use callback for this. – Pradeep Asanka Mar 16 '17 at 00:07
  • Check out this answer http://stackoverflow.com/questions/4368946/jquery-callback-for-multiple-ajax-calls – I think you can apply it to your problem, good luck. – luckyape Mar 16 '17 at 02:57

0 Answers0