2016-11-10 81 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

相關問題