在回调中解析AJAX响应

时间:2013-05-10 02:26:13

标签: ajax coffeescript

我需要在AJAX回调中从一些HTML响应中访问一段数据。

在我的Coffeescript中,我想访问completion-id数据属性。当我执行以下操作时:

  $('.mark-completed')
    .bind "ajax:success", (event, data) ->
        console.log data
        console.log $(data)

我在控制台中获取数据:

<li class="learning_item_completion" data-completion-id="162" data-user-id="2" id="learning_item_completion_162">
  <div class="item-icon">
    <img alt="Thumb_alex_headshot_green_200x200" class="circular" height="60" src="/uploads/user/picture/2/thumb_alex_headshot_green_200x200.png" width="60" />
  </div>
  <div class="item">

这是$(数据):

[li#learning_item_completion_162.learning_item_completion, jquery: "1.9.1", constructor: function, init: function, selector: "", size: function…]
  0: li#learning_item_completion_162.learning_item_completion
  accessKey: ""
  attributes: NamedNodeMap
  0: class
  1: data-completion-id
    ...
    nodeValue: "162"
    value: "162"
    ...

访问completion-id的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

只需添加:

$('.mark-completed')
.bind "ajax:success", (event, data) ->
    cid = $(data).find('.learning_item_completion').data('completion-id')
    console.log cid

答案 1 :(得分:0)

$.ajax({
  url: "test.html"
}).done(function( html ) {
  $("#results").append(html);
  console.log(html);
//etc.
});