Ajax 내에서 return 하면 undefined 현상.



제목과 같습니다. 

함수를 하나 제작해서 내부에서 ajax 를 사용하고,
ajax 값을 return 하려면 함수의 값이 undefined 로 나오는 현상이 발생합니다. 

Ajax 를 사용하고 내부에서 ( 가령 success ) 같은 곳에서 return 을 해주면 
제대로 return 되지 않습니다. 


>> 작동하지 않는 예시

function imgUploadfun() {

$.ajax({
url: "mainImageUpload.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: postData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData: false, // To send DOMDocument or non processed data file it is set to false
async: false,
success: function (json, aJaxtatus) // A function to be called if request succeeds
{
//var imageUploadJson = $.trim(json) ;
var imageUploadJson = JSON.parse(json);
if (imageUploadJson.status == "OK") {
mainImageUrl = imageUploadJson.url.slice(9, imageUploadJson.url.length);
// ajaxMainImg(mainImageUrl);
} else {
//alert(imageUploadJson.status);
}
return imageUploadJson.status;
},
error: function (e) {
alert("error");
}
});
};


>> 작동 하는 예시 

function imgUploadfun() {
var a ;

$.ajax({
url: "mainImageUpload.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: postData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData: false, // To send DOMDocument or non processed data file it is set to false
async: false,
success: function (json, aJaxtatus) // A function to be called if request succeeds
{
//var imageUploadJson = $.trim(json) ;
var imageUploadJson = JSON.parse(json);
if (imageUploadJson.status == "OK") {
mainImageUrl = imageUploadJson.url.slice(9, imageUploadJson.url.length);
// ajaxMainImg(mainImageUrl);
} else {
//alert(imageUploadJson.status);
}
a=imageUploadJson.status;
},
error: function (e) {
alert("error");
}
});

return a;
};

위와 같이 변수에 값을 넣어주고 ajax 외부에서 return 해주어야 합니다.