4

了解しました。増幅デコーダーの取得に取り組んでいます。奇妙な問題。リクエストにbeforeSendがアタッチされている場合、デコーダーは起動しません。beforeSendを削除すると、デコーダーが起動します。

これが2つの例です。

  1. beforeSendなし。

http://jsfiddle.net/sujesharukil/Td2P4/12/

  1. beforeSendで

http://jsfiddle.net/sujesharukil/Td2P4/14/

誰かが何が起こっているのか教えてもらえますか?beforeSendがある場合、デコーダーが機能しないのはなぜですか?リクエストを受信した後にデコーダーが起動するはずなので、beforeSendはそれに影響を与えないはずです!

注:stackoverflowでは、フィドルだけでなく、ここにコードを投稿する必要があります

//please check the fiddles

amplify.request({
    resourceId: "testRequest",
    data: {
        json: JSON.stringify({
            text: 'hello world'
        })
    },
    success: function(data, status) {
        console.log(data, status);
        $('.messages').append('<div> text retrieved: ' + data.text + '</div>');
    },
    error: function(status, xhr) {
        console.log(xhr);
    }
});​

ヘルプ?

-スージ

4

2 に答える 2

9

わかりました。

増幅jsで、このセクションが私の目に留まりました

beforeSend : function( _xhr, _ajaxSettings ) {

                xhr = _xhr;
                ajaxSettings = _ajaxSettings;
                var ret = defnSettings.beforeSend ?
                    defnSettings.beforeSend.call( this, ampXHR, ajaxSettings ) : true;
                return ret && amplify.publish( "request.before.ajax",
                    defnSettings, settings, ajaxSettings, ampXHR );
            }
        });

指定されている場合は beforeSend を呼び出し、指定されていない場合var retは set に設定されることに注意してください。true

true に設定すると、公開されます"request.before.ajax"

ファイルのずっと下で、amplify はこのメッセージをリッスンします

amplify.subscribe( "request.before.ajax", function( resource, settings, ajaxSettings, ampXHR ) {
var _success = ampXHR.success,
    _error = ampXHR.error,
    decoder = $.isFunction( resource.decoder )
        ? resource.decoder
        : resource.decoder in amplify.request.decoders
            ? amplify.request.decoders[ resource.decoder ]
            : amplify.request.decoders._default;

if ( !decoder ) {
    return;
}

function success( data, status ) {
    _success( data, status );
}
function error( data, status ) {
    _error( data, status );
}
ampXHR.success = function( data, status ) {
    decoder( data, status, ampXHR, success, error );
};
ampXHR.error = function( data, status ) {
    decoder( data, status, ampXHR, success, error );
};

});

そのため、beforeSend があり、それが true を返さない場合、メッセージは公開されず、デコーダーはヒットしません!

ソリューション?

beforeSend 関数から true を返す

amplify.request.define("testRequest", "ajax", {

url: "/echo/json/",
dataType: 'json',
type: 'POST',
decoder: function(data, status, xhr, success, error) {
    console.log('decoder fired');
    $('.messages').append('<div>decoder fired </div>');
    success(data);
},
beforeSend: function(xhr){
//not doing anything here, just logging;
    console.log('before send fired');
    $('.messages').append('<div>before send fired </div>');
    return true; //this is the key
}

});

魔法のように機能します!これが、これを理解しようとしている他の誰かに役立つことを願っています!

于 2012-11-09T18:27:06.953 に答える
0

これをサンプルページからコピーしただけです。それが役に立てば幸い。

amplify.request.decoders.appEnvelope =
    function ( data, status, xhr, success, error ) {
        if ( data.status === "success" ) {
            success( data.data );
        } else if ( data.status === "fail" || data.status === "error" ) {
            error( data.message, data.status );
        } else {
            error( data.message , "fatal" );
        }
    };

amplify.request.define( "decoderExample", "ajax", {
    url: "/myAjaxUrl",
    type: "POST",
    decoder: "appEnvelope" // <--- a function name(string) and not a function.
});

amplify.request({
    resourceId: "decoderExample",
    success: function( data ) {
        data.foo; // bar
    },
    error: function( message, level ) {
        alert( "always handle errors with alerts." );
    }
});
于 2012-11-09T18:04:55.180 に答える