0

現在、jquery の ajax 呼び出しに問題があります。私がやろうとしているのは、いくつかの div を空にし、コンテナーを非表示にしてから、要求された Ajax コードを呼び出して div をもう一度入力して表示することです。ただし、私のコードはリクエストを呼び出しており、ajaxリクエストの処理中にそれを非表示にして、divを空にしています...

カスタム コールバックを使用したサンプル コード (動作しない):

// This is defined within an element and is retrieving properly as well
cleanInfo(getCharacter($this.attr('id')));

function cleanInfo(callback){
    // hide the container first, then empty and callback to ajax function
$('.sel_information_container').hide('slide', {direction: 'down'}, 'fast',function(){
    $('.sel_info_top').html('');
    $('.sel_info_description').html('');
    $('.skill_tree').html('');
    callback;
       // OR
}, callback);   
}

function getCharacter(id){

if(!id){
    id = 0;
}

$.ajax({
// path is defined in core file this works right as it's retrieving the data properly... and everything is        
    // and everything is right in php...
       url : _path + "/core/ajax.php",
       type : 'POST',
       data : { f: 'getCharacter', i: id },
       dataType : 'json',
       success :  function(data) {
        // if data error is sent back process it ...
        if(data.error){

            $('body').html(data.error);

        }else{
            // if no error populate and show the container
            $('.sel_info_top').html(data.info);
            $('.sel_info_description').html(data.desc);
            $('.skill_tree').html(data.skills);
            $('.sel_information_container').show('slide', {direction: 'down'}, 'slow');         
        }

    }
});

}

ajaxSend/beforeSend/ajaxStart/always を使用したサンプル コード (動作しない):

// This is defined within an element and is retrieving properly as well
getCharacter($this.attr('id'));
// without callback and utilizing variable request...
function cleanInfo(){

$('.sel_information_container').hide('slide', {direction: 'down'}, 'fast',function(){
    $('.sel_info_top').html('');
    $('.sel_info_description').html('');
    $('.skill_tree').html('');
}); 
}

function getCharacter(id){

if(!id){
    id = 0;
}

    // Instead of attaching the results to the var, I have also ran beforeSend: cleanInfo() which 
    // does the exact same thing as the above...

var request = $.ajax({
// path is defined in core file this works right as it's retrieving the data properly... and everything is        
    // and everything is right in php...
       url : _path + "/core/ajax.php",
       type : 'POST',
       data : { f: 'getCharacter', i: id },
       dataType : 'json',
       success :  function(data) {

        if(data.error){

            $('body').html(data.error);

        }else{

            $('.sel_info_top').html(data.info);
            $('.sel_info_description').html(data.desc);
            $('.skill_tree').html(data.skills);
            $('.sel_information_container').show('slide', {direction: 'down'}, 'slow');         
        }

    }
});
    // ajaxSend, always, ajaxStart, all not working with this...
    request.ajaxSend(cleanInfo());

}

解決策はありますか?

4

1 に答える 1

0

ajax 呼び出しは非同期であるため、呼び出しはgetCharacterすぐに戻ります。

非表示部分と空にする部分を呼び出しに移動し、beforeSendエラーajax処理も呼び出しに追加しますajax

// This is defined within an element and is retrieving properly as well
cleanInfo($(this).attr('id'));

function cleanInfo(id){
   if(!id){
       id = 0;
   }

   $.ajax({
       url : _path + "/core/ajax.php",
       type : 'POST',
       data : { f: 'getCharacter', i: id },
       dataType : 'json',
       cache: 'false',
       beforeSend: function() {
                   console.log("Emptying");
                   // hide the container first, then empty and callback to ajax function
                   $('.sel_information_container').hide('slide', {direction: 'down'}, 'fast',function(){
                       $('.sel_info_top').html('');
                       $('.sel_info_description').html('');
                       $('.skill_tree').html('');
                   }
       },
       success :  function(data) {
            // if data error is sent back process it ...
            if(data.error){
                $('body').html(data.error);
            }else{
                // if no error populate and show the container
                console.log("Populating");
                $('.sel_info_top').html(data.info);
                $('.sel_info_description').html(data.desc);
                $('.skill_tree').html(data.skills);
                $('.sel_information_container').show('slide', {direction: 'down'}, 'slow');         
            }
       },
       error: function (xhr, textStatus, thrownError) {
         $('body').html("Error: " + xhr.status + " - " + thrownError);
       }
   });
}
于 2013-01-09T00:15:30.310 に答える