locked
Return object from AJAX request RRS feed

  • Question

  • User439975351 posted

    HI guys,

    I have a function that I want to pass back the response object but being async this passes back undefined. How can I achieve this and get the object back to the caller?

    Thanks,

    Jus

       var check = checkConstraints(reqData);

    if(check.IsValid == true){
    //Do this...
    }

    function checkConstraints(reqData){ var request = $.ajax({ type: "POST", url: urlPrefix + "/check-constraints", data: reqData, dataType: "json" }); request.done(function (data) { return data; //returns undefined }); }

    Friday, May 10, 2019 8:09 AM

Answers

  • User-474980206 posted

    the function should return a promise:

       var check = checkConstraints(reqData).done(function(check) {
          if(check.IsValid == true){
             //Do this...
          }
       }
    
       function checkConstraints(reqData){
           return $.ajax({
              type: "POST",
              url: urlPrefix + "/check-constraints",
              data: reqData,                      
              dataType: "json"
           }).done(function(data) {
              // process data
              var check = data;
              return check;
           });
       }
    

    if using modern javascript its:

       const check = await checkConstraints(reqData);
       if(check.IsValid == true){
             //Do this...
       }
       
    
       async function checkConstraints(reqData){
           const data = await $.ajax({
              type: "POST",
              url: urlPrefix + "/check-constraints",
              data: reqData,                      
              dataType: "json"
           });
    
           // process data
           let check = data;
           return check;
       }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 10, 2019 6:23 PM

All replies

  • User-474980206 posted

    the function should return a promise:

       var check = checkConstraints(reqData).done(function(check) {
          if(check.IsValid == true){
             //Do this...
          }
       }
    
       function checkConstraints(reqData){
           return $.ajax({
              type: "POST",
              url: urlPrefix + "/check-constraints",
              data: reqData,                      
              dataType: "json"
           }).done(function(data) {
              // process data
              var check = data;
              return check;
           });
       }
    

    if using modern javascript its:

       const check = await checkConstraints(reqData);
       if(check.IsValid == true){
             //Do this...
       }
       
    
       async function checkConstraints(reqData){
           const data = await $.ajax({
              type: "POST",
              url: urlPrefix + "/check-constraints",
              data: reqData,                      
              dataType: "json"
           });
    
           // process data
           let check = data;
           return check;
       }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 10, 2019 6:23 PM
  • User439975351 posted

    Thanks bruce, I have opted for the second option here which works perfectly. 

    All the best,

    Jus

    Monday, May 13, 2019 7:55 AM