Javascript jQuery对变量的ajax调用

Javascript jQuery对变量的ajax调用,javascript,jquery,ajax,cordova,Javascript,Jquery,Ajax,Cordova,对于我的Phonegap/Cordova项目,我尝试使用以下函数处理所有ajax调用: function requestData(action, id ) { var data = { user_id: localStorage.getItem('user_id') }; if( action == 'feed_load_feedings' || action == 'feed_add_load_menu_weight' || action == 'fee

对于我的Phonegap/Cordova项目,我尝试使用以下函数处理所有ajax调用:

function requestData(action, id ) {
    var data = {
        user_id: localStorage.getItem('user_id')
    };
    if( action == 'feed_load_feedings' || action == 'feed_add_load_menu_weight' || action == 'feed_add_load_menu_supps' ) {
        data.id = id;
    }
    $.ajax({
        type: 'post',
        url: 'http://_______.com/file.php?'+action,
        async: false,
        crossDomain: true,
        data: data,
        beforeSend: showLoader,
        error: handleError,
        success: handleSuccess
    });
}
function showLoader() {
    console.log('showLoader fired')
    $('body').prepend('<p id="loading">De gegevens worden geladen...</p>');
}
function handleError(data) {
    console.log('handleError fired')
    $('#loading').remove();
    $('body').prepend('<p id="error">Fout tijdens het laden van de gegevens...</p>');
    return false;
}
function handleSuccess(data) {
    console.log('handleSuccess fired')
    $('#loading').remove();
    if( typeof data !== 'object' ) {
        $('body').prepend('<p id="error">Fout in gegevens object...</p>');
        return false;
    }
    else if(data.length == 0 ) {
        return 0;
    }
    else {
        return data;
    }
}
在控制台中提供以下信息:

showLoader fired
POST http://_______.com/file.php?feed_load_feedings
handleSuccess fired
undefined

在POST请求中,我可以看到数据被调用,并且没有问题。但是,它没有被放入变量中。我认为将
async:false
添加到我的ajax调用中可以防止这种情况。我在监督什么?

您的代码运行得非常好。默认情况下,JavaScript函数没有返回值return
undefined
。函数
requestData
没有
return
语句,因此返回默认的
undefined

如果要保存数据,需要将其保存在
success
回调中


我的建议是不要尝试使用
async:false
。只要异步执行所有操作并适当地响应回调即可

返回undefined的原因非常明显,因为
requestData
函数不返回任何东西,这意味着它间接返回
undefined

ajax的流程是异步的,因此返回函数
requestData
中的任何内容都不起作用。您需要在函数调用中传递回调函数,并在成功/错误处理程序中执行它

$(function() {
    requestData('herd_load_herds', 'someid', function(err, data){
      if(err){
        console.log("error");
        console.log(err);
      }else{
        console.log("success");
        console.log(data):
      }
    });    
});
在AJAX中:

function requestData(action, id, callback) {
    var data = {
        user_id: localStorage.getItem('user_id')
    };
    if( action == 'feed_load_feedings' || action == 'feed_add_load_menu_weight' || action == 'feed_add_load_menu_supps' ) {
        data.id = id;
    } 
    $.ajax({
            type: 'post',
            url: 'http://_______.com/file.php?'+action,
            async: false,
            crossDomain: true,
            data: data,
            beforeSend: showLoader,
            error: function(error){
              handleError(error, callback);
            },
            success: function(data){
              handleSuccess(data, callback);
            }
        });
}
在处理程序中:

function handleSuccess(data, next) {
    console.log('handleSuccess fired')
    $('#loading').remove();
    if( typeof data !== 'object' ) {
        $('body').prepend('<p id="error">Fout in gegevens object...</p>');
        next(undefined, false);
    }else{       
      next(undefined, data);
    }
}

function handleError(err, next) {
    console.log('handleError fired')
    $('#loading').remove();
    $('body').prepend('<p id="error">Fout tijdens het laden van de gegevens...</p>');
    next(err);
}
函数handleSuccess(数据,下一步){
console.log('handleSuccess-fired')
$(“#加载”).remove();
if(数据类型!=='object'){
$('body').prepend('

Fout在gegevens对象中…

'); 下一个(未定义,错误); }否则{ 下一步(未定义,数据); } } 函数句柄错误(错误,下一个){ console.log('handleError-fired') $(“#加载”).remove(); $(“body”).prepend(“

Fout tijdens het ladden van de gegevens…

”); 下一个(错误); }
这很有意义。但是,如何将
success
回调中的数据获取到
herd
变量中呢?“我的建议是不要尝试使用
async:false
”进行攻击。我已经读过了,我和你在一起。但是我想处理来自这些脚本的所有ajax请求。所以我有一些改变,我只需要改变一次。我怎样才能做到这一点呢?让
herd
全局化,然后在
success
回调中保存到它(
herd=data
),谢谢你的努力。我尝试了你的代码,但它给了我一个错误:
TypeError:next不是函数
。。。怎么会?
function handleSuccess(data, next) {
    console.log('handleSuccess fired')
    $('#loading').remove();
    if( typeof data !== 'object' ) {
        $('body').prepend('<p id="error">Fout in gegevens object...</p>');
        next(undefined, false);
    }else{       
      next(undefined, data);
    }
}

function handleError(err, next) {
    console.log('handleError fired')
    $('#loading').remove();
    $('body').prepend('<p id="error">Fout tijdens het laden van de gegevens...</p>');
    next(err);
}