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

时间:2015-01-22 16:53:13

标签: javascript jquery ruby-on-rails ajax coffeescript

  

我的问题是:为什么我的ajax调用导致调用失败回调 - 而不是完成/成功,状态为200且全部?

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)

当我点击“按钮”时,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);

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

Heureka!

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

Ryan K的答案是正确答案 - 如果我使用成功错误回调而不是.done和.fail Promises!

在使用jQuery Promises时,强制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)