0

I am having a hard time understanding the $.getJSON.
I have a url and I want to make a function for example fetch(url) where I use $.getJSON to fetch a string and parse it.

The response of the url is in this form:

{
    "status": "Unknown",
    "additionalStatus": "None",
    "emailAddressProvided": "test@tester.com",
    "emailAddressChecked": "test@tester.com",
    "emailAddressSuggestion": ""
}

I want the function to return a var data where

data.status = Unknown 
data.additionalStatus = None
.
.

... and so on.

Thank you!

Andreas
  • 21,535
  • 7
  • 47
  • 56
outmane
  • 9
  • 1
  • 4
    `$.getJSON` uses AJAX. You *cannot* return from an AJAX call. It's *asynchronous*. You need to use a callback. – gen_Eric Sep 04 '15 at 16:51
  • See: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – gen_Eric Sep 04 '15 at 16:53
  • 1
    As @RocketHazmat said, getJSON returns an object which will update to include the data when the response is sent, but not immediately (only functions like window.confirm actually ever block javascript from running). You need to provide a "callback" function to the getJSON function, which it will call once the AJAX request has been responded to. – Oli Beatson Sep 04 '15 at 17:06

1 Answers1

0

function fetch(){ var output; $.ajax({ url: "put url here", datatype: "json", async: false }) .done(function(data){ output = "hi"; }); return output; }

Maybe you could do something like this to return an output. It however is not recommended to put to set async to false.

Hiltje
  • 140
  • 1
  • 5