$(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.
-
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
1 Answer
Reset to default 6This 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)
...
});