如何获取 function2 返回的 function1 中的字符串值
我如何获取函数2中返回的函数1中的字符串值,即
function fnc1(){
var text = fnc2("Pencils");
alert(text);
}
function fnc2(mytext){
$.post("process.php", {t:mytext}, function(data){
return data;
});
}
在process.php中
<?php
echo $_POST['t'];
?>
它返回未定义。
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:mytext}, function(data){
return data;
});
}
In process.php
<?php
echo $_POST['t'];
?>
it is returning undefined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能从这样的 ajax 请求中返回值。唯一可能的方法是将 async 设置为 false,然后将响应保存到变量并在 ajax 调用之外返回该响应。但这可能不是您想要的,因为它会在等待响应时锁定。
您可以将要执行的功能传递给第二个方法,如下所示:
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: