开发者

how can i get value of string in function1 returned in function2

how can i get value of string in function1 returned in function2 i.e

function fnc1(){
var text = fnc2("Pencils");
alert(text);
}

function fnc2(mytext){
$.post("process.php", {t:myte开发者_StackOverflow中文版xt}, function(data){
return data;
});
}

In process.php

<?php
echo $_POST['t'];
?>

it is returning undefined.


You can't return values from ajax requests like that. The only way that would be possible is to set async to false and then save the response to a variable and return that outside the ajax call. But this probably isn't what you want as it will lock while waiting for the response.

You could pass the function you want to perform to the 2nd method like this:

function fnc1(){
   fnc2("Pencils", function(data) { alert(data) });
}

function fnc2(mytext, callback){
   $.post("process.php", {t:mytext}, callback);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜