0

Is it possible to return the value from a REST response back to the calling function?

In the code below returneddata is always undefined. In the GetMyData function I can see the REST response and pick off an element...in this case element 1. But, when I try to return the value for further processing it vanishes. The var returneddata is undefined.

Is there a way around this? I'm trying to make my REST calls more general.

Thanks.

function GetItems()
{
var returneddata=GetMyData();
}
 
GetMyData: function()
    {
         $.ajax({    
            url: oDataUrl,    
            type: "GET",    
            dataType: "json",    
            headers: ="accept": "application/json;odata=verbose",    
            success: function (data) 
            {
                var mydata=data.d.results;
                console.log("success");
                console.log(data.d.results[1]);
                console.log(mydata);
                return mydata;   

                
            },
            error: function (error) {
                console.log('fail');
            }
        });    
        
    },

1 Answer 1

0

Yes, you just need to return the $.ajax() call itself.

GetMyData: function(){
   return $.ajax({
        ....

That returns a "thenable" value (kinda a promise, but jQuery's version of it).

1
  • Sweet!!! Thanks.
    – Pablo
    Feb 18, 2021 at 18:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.