4

最初の関数を考えてグローバル変数を設定しようとしましたが、たとえば、値を取得しようとするselectedDatesと失敗します。selectedDates['1/23/2013']

$.getの代わりに$.ajaxを試してみると、のような値を取得できますselectedDates['1/23/2013']

the_selected_datesそれらの両方がセットに返される外部変数を設定する場合の違いはどこにありselectedDatesますか?

var selectedDates = {};

使用する関数$.getは失敗します:

function getSelectedDates_fails(lead_id, month) {
    var the_selected_dates = {};
    $.get(
            window.location.href,
            {
                gf_lm_ajax : 1,
                get : 'lead_reminder_get_dates',
                lead_id : lead_id,
                month : month,
                nonce_gf_lm_ajax : gf_lmJsVars.nonce_gf_lm_ajax

            },
    function(output) {
        $.each(output.reminders, function(n, val) {
            the_selected_dates[val.date] = val.date;
        });
    }, 'json');
    return the_selected_dates;
}
selectedDates = getSelectedDates_fails(35, 12); console.debug(selectedDates);

デバッグデータ

ここに画像の説明を入力してください

ここに画像の説明を入力してください

作品を使っ$.ajaxた機能:

function getSelectedDates_works(lead_id, month) {
    var the_selected_dates = {};
    $.ajax(
            {
                url : window.location.href,
                dataType : 'json',
                data : {
                    gf_lm_ajax : 1,
                    get : 'lead_reminder_get_dates',
                    lead_id : lead_id,
                    month : month,
                    nonce_gf_lm_ajax : gf_lmJsVars.nonce_gf_lm_ajax
                },
                async : false,
                success : function(output)
                {
                    $.each(output.reminders, function(n, val) {
                        the_selected_dates[val.date] = val.date;
                    });
                }
            });
    return the_selected_dates;
}
selectedDates = getSelectedDates_works(35, 12); console.debug(selectedDates);

デバッグデータ

ここに画像の説明を入力してください ここに画像の説明を入力してください

4

4 に答える 4

4

$.ajax 同期して実行している間は、そうで$.getはありません。

そのgetSelectedDates_fails()ため、サーバーから応答を受け取るよりも速く戻り、空のオブジェクトを返します{}

表示されているのは、リクエストが非同期selectedDatesで完了する前のオブジェクトの状態です。


を使用して、すべての呼び出し$.ajaxSetup()の動作をグローバルに変更できます。$.ajaxただし、に設定asyncする場合はお勧めしませんfalse

于 2012-12-10T18:05:23.897 に答える
2

あなたはaysnc: falseあなたの中にあります、それはあなたが直接$.ajaxオンにすることができない設定です-あなたはを使わなければならないでしょう。それでも、それはおそらくあなたがやりたいことではありませんが、ajaxリクエストが非同期で実行されている場合、ajaxリクエストが完了する前に到達します。代わりに、コールバックでデバッグする必要があります。$.get$.ajaxSetupreturn the_selected_dates;

于 2012-12-10T18:07:44.500 に答える
2

を設定async: falseすると、メイン関数が戻る前に成功ハンドラーが最初に実行されます。それが機能する理由です。ただし、呼び出しの実行中にブラウザがハングするため、これはお勧めしません。

getSelectedDates_works()結果が処理されたときに呼び出されるコールバック関数を関数に渡すことにより、非同期で行う必要があります。このようにして、ブラウザは要求と応答の間に「休止」します。

function getSelectedDates_works(lead_id, month, cb) 
{
    $.ajax({
        // ...
        success: function(output) {
            var the_selected_dates = [];
            $.each(output.reminders, function(n, val) {
                 the_selected_dates[val.date] = val.date;
            });
            // perform callback
            cb(the_selected_dates);
        }
    });
}

getSelectedDates_works(123, 456, function(dates) {
    // do stuff with dates here
});
于 2012-12-10T18:10:09.600 に答える
2

データを非同期で取得し、返されたデータで何かを行うことにした場合は、ajax関数が実行されていることを確認する必要があります。

function getSelectedDates(lead_id, month) {
    return $.ajax({
        url: window.location.href,
        dataType: 'json',
        data: {
            gf_lm_ajax: 1,
            get: 'lead_reminder_get_dates',
            lead_id: lead_id,
            month: month,
            nonce_gf_lm_ajax: gf_lmJsVars.nonce_gf_lm_ajax
        }
    });
}

getSelectedDates(35, 12).done(function(output) {
    var the_selected_dates = {};
    $.each(output.reminders, function(n, val) {
        the_selected_dates[val.date] = val.date;
    });
    console.log(selectedDates);
});​
于 2012-12-10T18:16:19.350 に答える