coffeescripted ajax 调用结果在实际成功时回调失败

coffeescripted ajax call result to fail callback when actually success

My question is: Why does my ajax call result in the fail callback being called - and not the done/success, with the status being 200 and all?

post_success = (response) ->
  $('button.update').hide()
  $('#status_of_employees').html(response)

post_error = (response) ->
  console.log(response)
  $('#status_of_employees').html(response.responseText)

post_changes = (url,data) ->
  jqxhr = $.ajax
    url: url
    type: 'POST'
    data: data
  .done (response,st) ->
    post_success(response)
  .fail (response) ->
    post_error(response)

$('button').on 'click', (e) ->
  data = { 'multi': true, 'elems': [] }
  url = '/entrances.js'
  for elem in $('.deleted, .selected')
    do(elem) ->
      #
      # employee_1_date_1_9_entrance_0
      prop = $(elem).closest('tr').prop('id').split("_")
      data.elems.push { type: $(elem).prop('class'), employee: prop[1], month: prop[3], day: prop[4], entrance: prop[6] }
  post_changes('/entrances.js',data)

当我点击 'button' 时,ajax 调用被完美调用 - 在我的 Rails 控制器中,我响应

respond_to do |format|
  format.js   { render text: 'alt er opdateret'}
end

我的浏览器(Chrome 和 Safari 测试)看到的响应是这样的:

Object {
  readyState: 4, 
  getResponseHeader: function, 
  getAllResponseHeaders: function, 
  setRequestHeader: function, 
  overrideMimeType: function
  abort: function ( statusText )
  always: function () {
  complete: function () {
  done: function ()
  error: function ()
  fail: function ()
  getAllResponseHeaders: function ()
  getResponseHeader: function ( key )
  overrideMimeType: function ( type )
  pipe: function ( /* fnDone, fnFail, fnProgress */ )
  progress: function ()
  promise: function ( obj )
  readyState: 4
  responseText: "alt er opdateret"
  setRequestHeader: function ( name, value )
  state: function ()
  status: 200
  statusCode: function ( map )
  statusText: "OK"
  success: function ()
  then: function ( /* fnDone, fnFail, fnProgress */ )
  __proto__: Object

更新 23-01-15

这种语法

post_changes = (url,data) =>
  jqxhr = $.ajax
    url: url
    type: 'POST'
    data: data,
    done: (response,st) ->
      post_success(response)
    fail: (response) ->
      post_error(response)

生成此 js

      post_changes = (function(_this) {
        return function(url, data) {
          var jqxhr;
          return jqxhr = $.ajax({
            url: url,
            type: 'POST',
            data: data,
            done: function(response, st) {
              return post_success(response);
            },
            fail: function(response) {
              return post_error(response);
            }
          });
        };
      })(this);

更新 23-01-15 10:59

现在我相信语法(和 js)是正确的 - 这个语法

post_changes = (url,data) =>
  jqxhr = $.ajax(
    url: url
    type: 'POST'
    data: data
  ).done( (response,st) ->
    post_success(response)
  ).fail (response) ->
    post_error(response)

生成此 js

post_changes = (function(_this) {
  return function(url, data) {
    var jqxhr;
    return jqxhr = $.ajax({
      url: url,
      type: 'POST',
      data: data
    }).done(function(response, st) {
      return post_success(response);
    }).fail(function(response) {
      return post_error(response);
    });
  };
})(this);

但我仍然被调用失败回调 - 即使响应如上面的原始 post 所示:(

我不确定,但我认为你的语法有误。尝试:

post_changes = (url,data) ->
  jqxhr = $.ajax
    url: url
    type: 'POST'
    data: data
    done: (response,st) ->
      post_success(response)
    fail: (response) ->
      post_error(response)

Heureka!

我要将 Ryan K's 答案标记为正确答案 - 尽管它不是 100% 正确!然而,它确实促使我尝试了一些语法更改!

Ryan K 的回答将是正确的答案——如果我使用 successerror 回调而不是 .done 和 .fail承诺!

在使用 jQuery Promise 时,强制 CoffeeScript 构建正确的 JavaScript 需要一些特殊的语法。

开始了,

post_changes = (url,data) ->
  jqxhr = $.ajax(
    url: url
    type: 'POST'
    dataType: 'script'
    data: data
  ).done( () ->
    $('button.update').hide()
    $('#status_of_employees').html(jqxhr.responseText)
  ).fail () ->
    $('#status_of_employees').html(jqxhr.responseText)