jQuery的;我怎样才能返回ajax返回的数据? [英] jQuery; How can I return ajax returned data?

查看:131
本文介绍了jQuery的;我怎样才能返回ajax返回的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数reqNodelistByField(i){
$ .ajax({
type:'post',
url:'./req.nodelist.by.field.php',
dataType:'text',
data :{field_id:i},
成功:function(data){
r = $ .parseJSON(data);

if(r.valid === false)r =假;
}
});
alert(r);

return r;
};

回应 data 是完美的。但是我不知道如何在函数外面返回这个 data



只有我可以从 alert(r) undefined



如何返回警报(r)是不确定的,因为您正在执行演示程序。 ?

解决方案

一个异步调用,换句话说,您的脚本进行调用并继续执行代码,而无需等待调用的响应。为此,您需要在ajax成功函数中返回 r

 函数reqNodelistByField(i){
$ .ajax({
type:'post',
url:'./req.nodelist.by.field.php',
dataType:'text',
data:{field_id:i},
success:function(data ){
r = $ .parseJSON(data);

if(r.valid == false){
return false
}
else {
返回true
}
}
});
};


I'm trying to return AJAX response data like below..

        function reqNodelistByField( i ) {
            $.ajax({
                type: 'post',
                url: './req.nodelist.by.field.php',
                dataType: 'text',
                data: { field_id:i },
                success: function(data) {
                    r = $.parseJSON(data);

                    if(r.valid === false) r = false;
                }
            });
            alert(r);

            return r;
        };

Response data is perfect. However I don't know how to return this data outside the function.

Only I can see from alert(r) is undefined.

How can I return data?

解决方案

alert(r) is undefined because you are perfoming an asynchronous call, in other words, your script makes the call and continue executing the code without wait for call's response. For that, you need return the r in the ajax success function.

 function reqNodelistByField( i ) {
            $.ajax({
                type: 'post',
                url: './req.nodelist.by.field.php',
                dataType: 'text',
                data: { field_id:i },
                success: function(data) {
                    r = $.parseJSON(data);

                    if(r.valid == false) {
                         return false
                    }
                    else{
                         return true
                     }
                }
            });
        };

这篇关于jQuery的;我怎样才能返回ajax返回的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆