0

Edit: could've researched better... reading this post now: How do I return the response from an asynchronous call?

I have an ajax request which returns JSON data. When I watch it in fiddler, it does go out to the service and get the JSON data, but when I try to set a variable to it's response, that variable is "undefined". If I alert in the success method, it alerts, but the variable is still undefined.

I tried changing the function(data) to function(something) incase that had anything to do with it... same story.

var returndata
$.ajax({
    type: "GET",
    url: "GetSecurables/",
    data: { etaNumber: etaNumber },
    success: function (data) {
        returndata = data; //undefined
        alert('haaalp');
    }
});

The JSON is like below

[
  {
    "DelegateSid":null,
    "DisplayName":"Tom",
    "HasDelegation":true,
    "HasEtaManagement":false
  },
  {
    "DelegateSid":null,
    "DisplayName":"Tim",
    "HasDelegation":true,
    "HasEtaManagement":false
  },
  {
    "DelegateSid":null,
    "DisplayName":"Jake",
    "HasDelegation":true,
    "HasEtaManagement":false
  },
  {
    "DelegateSid":null,
    "DisplayName":"Ryan",
    "HasDelegation":true,
    "HasEtaManagement":false
  }
]
Community
  • 1
  • 1
Tom
  • 2,180
  • 7
  • 30
  • 48

1 Answers1

1

Try:

var returndata;

$.ajax({
    type: "GET",
    url: "GetSecurables/",
    data: { etaNumber: etaNumber },
    success: function (data) {
        console.log(data);
        returndata = data; 
        console.log(returndata);
    }
});

If the 2 outputs are the same it might be the case that you're trying to access returndata from outside its scope, hence the undefined, or that you're accessing returndata before the Ajax call completes.