1

if i have two functions that been called from ajax and i want to pass what ever the first function return to a variable in the other and use it only just once ONLY ONE TIME

the code bellow loop with the returnResult() function $.each() and some of the json data are missing like there is no text in the "labe" : {}

 $.ajax({
          type: "POST",
          url: "WebService.asmx/MyEventData",
          data: "{}",
          dataType: "json",
          contentType: "application/json; charset=utf-8",
          success: function(data) {
          returnResult(data.d);  
         }

      });

       $.ajax({
          type: "POST",
          url: "WebService.asmx/Getlines",
          data: "{}",
          dataType: "json",
          contentType: "application/json; charset=utf-8",
          success: function(data) {
              lineReturn(data.d);
          }

      });



      });

      function lineReturn(result) {
      var jsonObj = [] ; 
      $.each(result, function(index, value) {
         jsonObj.push({ "value": value.dateTime, "color": '#D2691E', "width": 1,    "label": { "text": value.LineEvent} });
      });
      return JSON.stringify(jsonObj) ; << this is the return that should go to myline variable and be used on the highchart >>>> 

  }

      function returnResult(result) {
      console.log(result) ;
      var myLine = lineReturn(result) ;
      var datetemp = new Array ;
      var dateflow = new Array ;
      var datepressure = new Array;
      var datecond = new Array ; 

2 Answers 2

2

Try putting the 2nd ajax call in the callback of the 1st.

$.ajax({
      type: "POST",
      url: "WebService.asmx/Getlines",
      data: "{}",
      dataType: "json",
      contentType: "application/json; charset=utf-8",
      success: function(dataA) {
           $.ajax({
               type: "POST",
               url: "WebService.asmx/MyEventData",
               data: "{}",
               dataType: "json",
               contentType: "application/json; charset=utf-8",
               success: function(dataB) {
                   // From here you can access both dataA.d and dataB.d
                   var myLineA = lineReturn(dataA.d);
                   var myLineB = lineReturn(dataB.d);
              }
           });
      }
});
Sign up to request clarification or add additional context in comments.

2 Comments

okay can i at the second sucess add a function like this function dosomething(dataA.d, dataB.d){ then $.each(dataA .... then $.each(dataB ...... is thats possible
@MinaGabriel: That should work. In the 2nd success, you can access both dataA and dataB.
1

You can have the second function on .success of the first function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.