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);
}
精彩评论