Mohammad Faizan Khan Mohammad Faizan Khan - 2 months ago 20
Javascript Question

How to use jquery when function with Get Request

I have this function that requesting and getting data from server

function TaskFinishedRequestAccepted(requestId, empTaskAssignCompletionId, selectedFinishedTaskMiainId, selectedFinishedTaskStautsDetail, requestByEmpId){
var go_path = "ESP.php?action=tfrq&vars=5&var1=" + requestId +
"&var2=" + empTaskAssignCompletionId + "&var3=" + selectedFinishedTaskMiainId + "&var4=" + selectedFinishedTaskStautsDetail + "&var5=" + requestByEmpId;
$jq.get(go_path,{},function(data) {

});

}


And I am calling this function in order to get it's return data in this way,
code given below (which is wrong, seeking help for it)

$.when(TaskFinishedRequestAccepted(2,3,4,5,33)).done(function(return){
console.log(return);
});


But I don't know how to return actually? So that I can get its response in done function.

Answer Source

Simply return the deferred object from your function. There's no need to use when. Also, jQuery offers a much more convenient way to construct URL query parameters...

return $jq.get('ESP.php', {
  action: 'tfrq',
  vars: 5,
  var1: requestId,
  var2: empTaskAssignCompletionId,
  var3: selectedFinishedTaskMiainId,
  var4: selectedFinishedTaskStautsDetail,
  var5: requestByEmpId
})

and

TaskFinishedRequestAccepted(2,3,4,5,33).done(function(res) {
  console.log(res);
});