简体   繁体   English

Ajax返回不触发回调

[英]Ajax return not firing Callback

I'm trying to troubleshoot some code that I did not write and I'm having a lot difficulty trying to figure out why an Ajax return is not firing a Callback. 我正在尝试对一些我未编写的代码进行故障排除,并且在试图弄清楚为什么Ajax返回未触发回调时遇到很多困难。 Here is the code that attaches the behaviors to the ajax functions: 这是将行为附加到ajax函数的代码:

  # Callback before AJAX request sends
cbBeforeSend = (jqXHR, settings) ->
  console.log jqXHR
  # initialize message/status elements
  $flashIcon.attr 'class', 'icon icon-refresh icon-spin'
  $flashError.html ''
  $flashNotice.html ''

# Callback when AJAX returns with success
cbSuccess = (data, textStatus, jqXHR) ->
  console.log 'success'
  $flashIcon.attr 'class', 'icon icon-ok-sign'
  window.globalLoadCallback()

# Callback when AJAX returns with error
cbError = (jqXHR, textStatus, errorThrown) ->
  console.log 'error'
  $flashIcon.attr 'class', 'icon icon-remove-circle'
  # Run the response javascript, even when the status indicates an error
  if /text\/javascript/.test jqXHR.getResponseHeader('Content-Type') 
    eval jqXHR.responseText

# Callback when AJAX returns
cbComplete = (jqXHR, textStatus) ->
  console.log 'cbComplete'
  if $flashIcon.is('.icon-refresh')
    $flashIcon.attr 'class', 'icon icon-warning-sign'

Our application has two links to the Quoting/Quote path: 我们的应用程序具有两个指向“报价/报价”路径的链接:

The new quote link works and fires the 'success' and 'cbComplete' callbacks I've shown above. 新的引号链接有效并触发我在上面显示的'success'和'cbComplete'回调。 Here is the code for that link: 这是该链接的代码:

<li>
  <%= link_to new_quoting_quote_path, remote:true do %>
    <i class="icon icon-plus-sign"></i>
    <span>New Quote</span>
  <% end %>
</li>

The edit quote link works, it brings you to the correct partial and the object gets returned in the console, but the 'success' and 'cbComplete' callbacks aren't firing (error isn't either). 编辑引号链接有效,它将您带到正确的部分,并且该对象在控制台中返回,但是'success'和'cbComplete'回调没有触发(错误也不是)。 Only the 'cbBeforeSend' callback is firing because the classes on the flashIcon remain "icon icon-refresh icon-spin". 仅触发“ cbBeforeSend”回调,因为flashIcon上的类仍为“ icon icon-refresh icon-spin”。 I don't believe there is a problem with crm_connection because in browser the link is rendered as "/quoting/quotes/183/edit". 我不相信crm_connection有问题,因为在浏览器中,链接呈现为“ / quoting / quotes / 183 / edit”。 The correct ID seems to be supplied. 似乎提供了正确的ID。 Here is the code for that link: 这是该链接的代码:

<%= client_management_tab 'Health', edit_quoting_quote_path(@crm_connection) %>

Sorry I can't supply more details. 抱歉,我无法提供更多详细信息。 Unfortunately I did not write this code and am only tasked with fixing it. 不幸的是,我没有编写此代码,而只是负责修复它。 Thank you for any help you can provide. 感谢您提供任何帮助。

I beleive this is the code that attaches the requests: 我相信这是附加请求的代码:

  $flashInfo   = $('div.flash-info')
  $flashIcon   = $flashInfo.find('i#ajax-status')
  $flashError  = $flashInfo.find('#flash-error')
  $flashNotice = $flashInfo.find('#flash-notice')
  $(document).bind 'ajax:beforeSend', cbBeforeSendBound
  $(document).bind 'ajax:success', cbSuccessBound
  $(document).bind 'ajax:error', cbErrorBound
  $(document).bind 'ajax:complete', cbCompleteBound

$.ajaxSetup(
  beforeSend: cbBeforeSend
  success: cbSuccess
  error: cbError
  complete: cbComplete
  )

From the jQuery docs for ajaxSetup: 从ajaxSetup的jQuery文档中:

Description: Set default values for future Ajax requests. Its use is not recommended.

I'd suggest you change your call and assign the callbacks to your specific ajax request: 我建议您更改呼叫并将回调分配给您的特定ajax请求:

jQuery.ajax
    url: 'blah/blah',
    success: cbSuccess,
    error: cbError,
    etc

If the actual call to jQuery.ajax sets its own callback, then I am fairly certain it will ignore the ones provided by ajaxSetup. 如果对jQuery.ajax的实际调用设置了自己的回调,那么我可以肯定它将忽略ajaxSetup提供的回调。 This makes them pretty unreliable for anything but setting defaults, and as Ricardo pointed out in his answer, the jQuery developers recommend not to use this mechanism. 除了设置默认值之外,这使得它们几乎不可靠,并且正如Ricardo在他的回答中指出的那样,jQuery开发人员建议不要使用此机制。

You have a couple of options the way I see it. 我有几种选择。 There are a series of other ajax functions provided by jQuery for global ajax event binding: jQuery为全局ajax事件绑定提供了一系列其他ajax函数:

jQuery.ajaxStart, jQuery.ajaxStop, jQuery.ajaxComplete, jQuery.ajaxError, jQuery.ajaxSuccess, jQuery.ajaxSend jQuery.ajaxStart,jQuery.ajaxStop,jQuery.ajaxComplete,jQuery.ajaxError,jQuery.ajaxSuccess,jQuery.ajaxSend

If you bind the events using these functions then they will always be invoked by ajax calls through jQuery BUT this can also be overridden by setting the option "global:false" in the actual ajax request. 如果使用这些函数绑定事件,则将始终通过jQuery BUT进行ajax调用来调用事件,也可以通过在实际ajax请求中设置选项“ global:false”来覆盖它们。

If this is the case for you, then you are pretty much stuck with Ricardo's answer: modify the actual calls to jQuery.ajax to do the binding. 如果您是这种情况,那么您几乎会陷入里卡多的答案:修改对jQuery.ajax的实际调用以进行绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM