I want to return response data when XMLHttpRequest transaction completes successfully. upon calling my function trashAnswer() to a variable I expect to assign the value returned to the variable and do something with it like below:
I can see resData is always undefined when i try to access data from it. Please how can I solve this?
var resData = trashAnswer({answerid:58,answererid:65,questionid:458});
This is the code:
//This function will trash answer object
function trashAnswer(param){
if (typeof param !== 'object' || param == null ) {
throw "trashAnswer(): Strictly expect valid object.";
}
var param = $.extend({answerid:0,answererid:0},param),
fd = new FormData();
fd.append('answerData',JSON.stringify(param));
fd.append('trash-answer',true);
var req = AJAX_REQEUST_OB();
req.open(bigPipe.formMethod.a,ajax.ac,true);
req.onload = function(){
if (req.readyState === req.DONE && req.status === 200) {
//ParseJSON is a custom function to check if response is a valid JSON...if its valid then then function will return response Data else return false
var Data = ParseJSON(req.responseText);
//process data...
var EvalData = !Data ? function(){
throw "Invalid JSON";
}:function(Data){
//do something...
return Data;
};
//call EvalData ****method
EvalData(Data);
}
}
//send request.
req.send(fd);
}