jquery - amplifyjs 解码器和 ajax beforeSend

标签 jquery amplifyjs

好的,正在努力获取放大解码器。奇怪的问题。如果我的请求附加了 beforeSend,则解码器不会触发。删除 beforeSend 并触发解码器。

这是两个示例。

  1. 没有 beforeSend。

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

  1. 与 beforeSend 一起使用

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

谁能告诉我发生了什么事吗?如果我有 beforeSend,为什么解码器无法工作?我假设解码器应该在收到请求后触发,因此 beforeSend 不应对其产生任何影响!

注意:stackoverflow 希望我在这里发布代码,而不仅仅是 fiddle

<p>//please check the fiddles</p> <pre><code>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></pre>

帮忙?

-Suj

最佳答案

好的,明白了。

在 amplifyjs 中,这一部分引起了我的注意

<pre><code>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></pre>

注意,如果指定的话会调用beforeSend,否则设置<code>var ret</code>设置为<code>true</code>

如果设置为 true,则会发布 <code>"request.before.ajax"</code>

在文件中,amplify 监听此消息

<pre><code>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></pre> <p>});</p>

所以,如果你有一个 beforeSend 并且它没有返回 true,则消息永远不会发布,解码器也永远不会被命中!

解决方案?

从 beforeSend 函数返回 true

<pre><code>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></pre> <p>});</p>

就像魅力一样!希望这可以帮助其他试图解决这个问题的人!

关于jquery - amplifyjs 解码器和 ajax beforeSend,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13313439/

相关文章:

javascript - Jquery wrapInner 无法在页面上工作

javascript - MeteorJS 和 AmplifyJS 响应式(Reactive)

node.js - 通过 AWS Amplify 使用查询 API 获取所有项目

javascript - 如何在 Javascript 中将 JSON 转换为数组

jquery - 如何在不透明后获得真实的颜色值

Javascript 不向 html 元素添加类

javascript - 当另一个字段值更改时,如何使用 jQuery 更改字段

react-native - 在 React Native 项目中向 Amplify API 添加模拟时出错

javascript - aws-amplify-react 和 @aws-amplify/ui-react 有什么区别?