1

In Jquery Ajax ,Can we get response value as corresponding function return value

 checkSucess = function() { // Jquery Ajax with url,params and response doPost('sucess.php', 'first=' + first, function(response) { }); return response;// 'response' here is the value of response 

How to return ajax response value of a function ,which is called from other function

2 Answers 2

1

when it's asynchronus you can't do it the way you want
if your sure that it the request will not last very long then make it async: false and see

checkSucess = function() { var result; //make it synchronus $.ajaxSetup({async:false}); doPost('sucess.php', 'first=' + first, function(response) { //get the response result = response; }); //make it asynchronus $.ajaxSetup({async:true}); return result; } 
Sign up to request clarification or add additional context in comments.

2 Comments

If request takes too long ,then what would be result ?
the user navigator will hang for a while !! we gotta try to make a better workaround
0

You could define some global variable and assign the ajax response to it:

 var someGlobal; checkSucess = function() { // Jquery Ajax with url,params and response doPost('sucess.php', 'first=' + first, function(response) { someGlobal = response; }); return someGlobal; 

Does this work for you

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.