I have the following script:
var myData;
$.ajax({
url: "/foo.php",
dataType : 'json',
type: "POST",
async: false
})
.done(function( data ) {
myData = data;
});
All I want to do is assign the data from the ajax response to myData
and refer to this variable further down in my script. The above code seems to work but relies on async: false
which I've read is not a good thing. If I comment this out (and therefore it uses the default async: true
) nothing is assigned to myData
.
I've read the following post jQuery ajax success callback function definition but can't adapt that so I can access the data in myData
. I also note that question was asked in 2013 so not sure if it's still accurate or the right way to go about this?
How is this supposed to be done? I'm using jquery v1.11.1
See Question&Answers more detail:os