Javascript jQuery返回未定义的POST调用

Javascript jQuery返回未定义的POST调用,javascript,jquery,post,Javascript,Jquery,Post,我相信这是件很容易的事,但当我被这些事情困住的时候,我就是个傻瓜。。。已经两个多小时了,我讨厌发生这种事:( 为什么返回未定义 function userExists(user) { $.post("misc/user_exists.php", {user: user}, function(result) { return '' + result + ''; }); }); php文件返回的用户名非常好,正如我在firebug的响应中看

我相信这是件很容易的事,但当我被这些事情困住的时候,我就是个傻瓜。。。已经两个多小时了,我讨厌发生这种事:(

为什么返回未定义

function userExists(user) {
$.post("misc/user_exists.php",  {user: user},
        function(result) {
            return '' + result + '';
        });
});
php文件返回的用户名非常好,正如我在firebug的响应中看到的那样。但是这个函数没有用,当我在调用它之后发出警报时,它总是未定义的,无论我返回的是字符串还是布尔值,等等


谢谢!

您需要为jQuery post命令添加正确的数据类型参数。 参考:

您需要为jQuery post命令添加正确的数据类型参数。 参考:

AJAX success函数中的
$.post
请求是异步的,因此当您运行
返回“”+结果+“”;
时,它实际上不会将数据返回到任何地方。相反,从AJAX success函数中触发不同的事件。

$.post
请求是异步的,因此当您运行
返回“”+结果+“”;
时,它不是异步的实际上,将数据返回到任何位置。相反,从AJAX成功函数内部触发不同的事件。

问题是,返回只会影响来自post方法的AJAX.success回调。请尝试以下操作:

function userExists(user, callback) //because this can be async, you need a callback...
{
    $.post("misc/user_exists.php",  {user: user},
    function(result) 
    {
        if(callback)
            callback(result)
        return '' + result + '';
    });
};
userExists('someUser', function(result)
{
     alert('' + result + '')
});
或者,您可以确保调用不是异步的:

function userExists(user) //because this can be async, you need a callback...
{
    var res;
    $.post("misc/user_exists.php",  {user: user},
    function(result) 
    {
        res = result;
    });
    return res;
};
$.ajax({async:false}); 
alert('' + userExists('someUser'+ '');    

问题是返回只会影响来自post方法的ajax.suces回调。请尝试以下操作:

function userExists(user, callback) //because this can be async, you need a callback...
{
    $.post("misc/user_exists.php",  {user: user},
    function(result) 
    {
        if(callback)
            callback(result)
        return '' + result + '';
    });
};
userExists('someUser', function(result)
{
     alert('' + result + '')
});
或者,您可以确保调用不是异步的:

function userExists(user) //because this can be async, you need a callback...
{
    var res;
    $.post("misc/user_exists.php",  {user: user},
    function(result) 
    {
        res = result;
    });
    return res;
};
$.ajax({async:false}); 
alert('' + userExists('someUser'+ '');    
你可以这样做

function userExists(user) {
    var username = '';
$.post("misc/user_exists.php",  {user: user},
        function(result) {

            username = result ;
        });
   return username;
});
你可以这样做

function userExists(user) {
    var username = '';
$.post("misc/user_exists.php",  {user: user},
        function(result) {

            username = result ;
        });
   return username;
});

result
是否未定义?或者
userExists(user)
的返回值是否未定义?请尝试:console.log(result);Firebug说什么?userExists(user)的结果。result是存在时的用户名。result在函数内部可以,但外部不起作用,我的意思是在调用userExists(user)时它给我一个未定义的提示。但是如果我在函数中添加了一个提示,那么result被正确提示(用户名称)是
result
undefined?或者
userExists(user)
undefined?Try:console.log(result);Firebug说什么?userExists(user)的结果.result是存在时的用户名。result在函数内部没有问题,但外部不起作用,我的意思是,当我调用userExists(user)时,它会给我未定义的消息。但是如果我在函数内部放置警报,result会被正确地警报(用户名)您的将返回“”而不是未定义的。调用是异步的。您的将返回“”而不是未定义的。调用是异步的。