2

.ajax() の呼び出しに新しい .done() 構文を使用しようとしていますが、サーバーから返されたデータを .done() 関数に取得する方法がわかりません。これが私のコードです:

function checkLink(element) {
    var resultImg = $(element).parent().parent().find("img");

    resultImg.attr("src", "/resources/img/ajaxLoad.gif");

    $.ajax({
        type: 'POST',
        url: '/services/Check.asmx/CheckThis',
        data: '{somedata: \'' + whatever + '\'}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: onSuccess,
        error: onFailure
    }).done(function () { success2(resultImg); });
}

function success2(img) {
    img.attr('src', '/resources/img/buttons/check.gif');
}

function onSuccess(data) {
    // The response from the function is in the attribute d
    if (!data.d) {
        alert('failed');
    }
    else {
        alert('hurray!');
    }
}

checkLink は、ボタンを押すだけで呼び出されます。onSuccess() と success2() の両方が正常に起動しています。しかし...私が必要としているのは、onSuccessからsuccess2に渡された「データ」パラメーターです...または、代わりに「resultImg」をonSuccessに渡すことができます(ただし、廃止されたメソッドの代わりに.doneを使用することをお勧めします)。独自のパラメーターを渡すか、AJAX 呼び出しからの JSON 結果にアクセスできるようですが、両方はできません。どうすればこれを達成できますか?

4

1 に答える 1

1

resultImg 変数を閉じることができます。

function checkLink(element) {
    var resultImg = $(element).parent().parent().find("img");

    resultImg.attr("src", "/resources/img/ajaxLoad.gif");

    $.ajax({
        type: 'POST',
        url: '/services/Check.asmx/CheckThis',
        data: '{somedata: \'' + whatever + '\'}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: onSuccess,
        error: onFailure
    }).done(success2);

    function success2(data) {
        resultImg.attr('src', '/resources/img/buttons/check.gif');
        // do whatever with data
    }

    function onSuccess(data) {
        // The response from the function is in the attribute d
        if (!data.d) {
            alert('failed');
        }
        else {
            alert('hurray!');
        }
    }
}
于 2013-03-18T23:28:45.447 に答える