最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Cannot read property 'createDocumentFragment' of undefined" - Stack Overflow

programmeradmin0浏览0评论
$(document).ready(function(){
    $(".item-title a").each(function(index) { 
        var yaz = $(this).attr("href");
        $.ajax({
            url: ';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.

$(document).ready(function(){
    $(".item-title a").each(function(index) { 
        var yaz = $(this).attr("href");
        $.ajax({
            url: 'https://api-metrica.yandex./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.

Share Improve this question edited Nov 22, 2017 at 20:54 Patrick Hund 20.3k12 gold badges71 silver badges95 bronze badges asked Nov 22, 2017 at 20:48 Melih511Melih511 1632 silver badges5 bronze badges 2
  • not suer if this matters but did you mean to have pagePath== with two equals? – Intervalia Commented Nov 22, 2017 at 20:51
  • @Intervalia this is metrica url parameter – Melih511 Commented Nov 22, 2017 at 20:53
Add a ment  | 

1 Answer 1

Reset to default 6

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)
   ...
});
发布评论

评论列表(0)

  1. 暂无评论