Actually set async: false
is a bad approach, quote from this answer:
It is generally a bad idea to make AJAX calls synchronous. DON'T DO
IT. I'm serious. I only mention it here for the sake of completeness.
Why is it bad do you ask?
JavaScript runs in the UI thread of the browser and any long running
process will lock the UI, making it unresponsive. Additionally, there
is an upper limit on the execution time for JavaScript and the browser
will ask the user whether to continue the execution or not. All of
this is really bad user experience. The user won't be able to tell
whether everything is working fine or not. Furthermore the effect will
be worse for users with a slow connection.
One way is to rewrite your code to:
function getImage() {
var imageX;
$.ajax({
type: 'GET',
url: 'php/my1.php',
dataType: 'json',
success: function(response){
imageX = response[0].studentName,
groupX = response[0].subjectId;
}
});
return imageX;
}
then you can pass it to a variable:
var result = getImage();
and get the data that is returned by the AJAX call like this:
result.success(function (data) {
alert(data);
});