Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot read property 'createDocumentFragment' of undefined"

$(document).ready(function(){
    $(".item-title a").each(function(index) { 
        var yaz = $(this).attr("href");
        $.ajax({
            url: 'https://api-metrica.yandex.com/analytics/v3/data/ga?end-date=today&ids=ga%3A35416355&dimensions=ga:pagePath&metrics=ga:pageviews&filters=ga:pagePath=='+yaz+'&start-date=2015-10-25&oauth_token=AQAAAAAVs-uLAASpEAf-MmJK_kHgpU9Fwv8WArM',
            type: 'get',
            dataType: "jsonp",
            success: function(data){  
                $(this).append(data.rows);
            }
        });
    });
});

Console: Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined

What is problem?
Please Help.

like image 383
Melih Atik Avatar asked Nov 22 '17 20:11

Melih Atik


1 Answers

This is because of the this context in success callback. It does not point to the jQuery object inside the callback as you expect. It will refer to the current context.

success: function(data){  
    $(this).append(data.rows);;
}

Save the context of this outside the success and reuse it.

var cachedThis = this;

$.ajax({
   ...
   success: function(data){  
      $(cachedThis).append(data.rows);;
   }
   ...
});

Instead you can use the bind method to lock the context.

$.ajax({
   ...
   success: function(data){  
       $(this).append(data.rows);;
   }.bind(this)
   ...
});
like image 87
Sushanth -- Avatar answered Sep 22 '22 15:09

Sushanth --