2013-05-10 85 views
0

我需要從AJAX回調中的某個HTML響應中訪問一段數據。在回調中解析AJAX響應

在我的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" 
    ... 

什麼是訪問的正確方法完成-ID?

回答

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

只需補充一點:

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