2016-11-10 86 views
1

问题似乎是财产 '文':遗漏的类型错误:无法读取的不确定(...)

$("#results").append("<div class='list-group'><a class='list-group-item' href='https://it.wikipedia.org/wiki/" + encodeURIComponent(item.title.replace(" ", "_")) + "' data-toggle='modal' data-target='.bs-example-modal-lg'><h4>" + item.title.replace(" ", "_") + "</h4><p class='list-group-item-text'>" + item.snippet + "</p></a></div>"); 

而且它的呼叫

page: e.relatedTarget.textContent, 

Hit the button Wikipedia, get the results, click on a result = should load its wikipedia page: JSFiddle playground

下面有它:

$("#wiki").one('click', function(e) { 
    var articleName = $(this).data('subject'); 
    $.getJSON("https://it.wikipedia.org/w/api.php?callback=?", { 
    srsearch: articleName, 
    action: "query", 
    list: "search", 
    format: "json" 
}, function(data) { 
    $("#results ul").empty(); 
    $("#results ul").append("<h3>Results for <b>" + articleName + "</b></h3>").text(); 
    $.each(data.query.search, function(i, item) { 
     $("#results").append("<div class='list-group'><a class='list-group-item' href='https://it.wikipedia.org/wiki/" + encodeURIComponent(item.title.replace(" ", "_")) + "' data-toggle='modal' data-target='.bs-example-modal-lg'><h4>" + item.title.replace(" ", "_") + "</h4><p class='list-group-item-text'>" + item.snippet + "</p></a></div>"); 
     $("#results div a").attr("href", "#"); 
    }); 
    $('.modal').on('show.bs.modal', function (e) { 
     $.getJSON("https://it.wikipedia.org/w/api.php?action=parse&format=json&callback=?", { 
      page: e.relatedTarget.textContent, 
      prop:"text" 
     }, function(data) { 
      $(".modal-content").html(data.parse.text['*']); 
     }); 
    }); 
}); 
}); 

尽管这个工程,但我需要上述的一个由于HTML无线薄呢:

$("#wiki").one('click', function(e) { 
    var articleName = $(this).data('subject'); 
    $.getJSON("https://it.wikipedia.org/w/api.php?callback=?", { 
    srsearch: articleName, 
    action: "query", 
    list: "search", 
    format: "json" 
}, function(data) { 
    $("#results ul").empty(); 
    $("#results ul").append("<h3>Results for <b>" + articleName + "</b></h3>").text(); 
    $.each(data.query.search, function(i, item) { 
     $("#results").append("<a class='list-group-item' href='https://it.wikipedia.org/wiki/" + encodeURIComponent(item.title.replace(" ", "_")) + "' data-toggle='modal' data-target='.bs-example-modal-lg'><h4>" + item.title.replace(" ", "_") + "</h4><p class='list-group-item-text'>" + item.snippet + "</a>"); 
     $("#results div a").attr("href", "#"); 
    }); 
    $('.modal').on('show.bs.modal', function (e) { 
     $.getJSON("https://it.wikipedia.org/w/api.php?action=parse&format=json&callback=?", { 
      page: e.relatedTarget.textContent, 
      prop:"text" 
     }, function(data) { 
      $(".modal-content").html(data.parse.text['*']); 
     }); 
    }); 
}); 
}); 

回答

1

问题是是给你整个文本 - 这是标题和描述。您只需要搜索标题即可找到结果。

看看开发工具中的网络选项卡,您将看到响应中出现“您指定的页面不存在”错误。

变化挑出只有H4内容:

page: $(e.relatedTarget).find('h4').text(), 

Updated fiddle

相关问题