Amplifier and ajax decoders beforeSend

Well, working on gain decoders. Weird question. If I have a beforeSend application attached to my request, the decoder does not start. remove the beforeSend command and the decoder will start working.

Here are two examples.

  • Without sending.

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

  • Using beforeSend

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

Can someone tell me what's going on? Why not work a decoder if I have it before sending? I assume that the decoder should work after receiving the request, so before sending it should not have any effect on it!

note: stackoverflow wants me to post code here, not just scripts

// 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); } });​ 

code>

reference

-Suj

+4
source share
2 answers

Ok, got it.

in amplifications this section caught my attention

 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 ); } }); 

code>

note that it will call beforeSend if it is specified, otherwise the var ret parameter is set to true

if set to true, it will publish "request.before.ajax"

down in the file, the gain listens for this message

 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 ); }; 

});

code>

therefore, if you have beforeSend and if it does not return true, the message will never be published and the decoders will never be deleted!

decision?

returns true from beforeSend function

 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 } 

});

code>

works like a charm! Hope this helps someone else try to figure it out!

+9
source

Just copy this from the sample pages. Hope this helps.

 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." ); } }); 
0
source

Source: https://habr.com/ru/post/1445118/


All Articles