2017-03-19 24 views
0

Ich habe den Fehler im Titel, während ich das ausführen. Ich versuche, den ersten Absatz ohne HTML analysiert zu bekommen, während dieseFehler: Eigenschaft 'Text' von undefined kann nicht gelesen werden

HTML

<div id="headingWiki_0"><h3><span>Roman empire</span></h3></div> 

JS

var titolo = $("#headingWiki_0 h3 span").text(); 
    $.getJSON("https://it.wikipedia.org/w/api.php?action=parse&format=json&prop=text&callback=?", { 
    page: titolo 
    }, function(data) { 
    var markupt = data.parse.text["*"]; 
    var blurbt = $('<div></div>').html(markup); 
    blurbt.find(".mw-editsection, #toc, .noprint, .thumb, img, table").remove(); 
    // remove links as they will not work 
    blurbt.find('a').each(function() { $(this).replaceWith($(this).html()); }); 
    // remove any references 
    blurbt.find('sup').remove(); 
    // remove cite error 
    blurbt.find('.mw-ext-cite-error').remove(); 
    $('#usp-custom-4').val($(blurbt).find('p')); 

    }); 

Console ausgeführt wird, sagt:

Uncaught TypeError: Cannot read property 'text' of undefined

+2

Zeigen das Markup Sie haben ... und ich denke, ein Punkt in dieser Regel müssen '$ ("headingWiki_0 h3 Spanne"). Text();.', Für die 'headingWiki_0', wie es scheint sei eine Klasse, kein Element – LGSon

+0

@LGSon welche genau markieren? –

+0

@ rob.m html Markup Sie suchen innerhalb – ilyaigpetrov

Antwort

0

Dank der ich von einem anderen so question bekam, ist dies der Code, der

$("#wiki").on("click", function(){ 
 
\t firstWiki(); 
 
}); 
 

 

 
function onSuccess(data){ 
 
     var markupt = data.parse.text["*"]; 
 
     $('#usp-custom-4').text(markupt); 
 
     console.log(markupt); 
 
     var blurbt = $('<div></div>').html(markupt); 
 
     blurbt.find(".mw-editsection, #toc, .noprint, .thumb, img, table").remove(); 
 
     // remove links as they will not work 
 
     blurbt.find('a').each(function() { $(this).replaceWith($(this).html()); }); 
 
     // remove any references 
 
     blurbt.find('sup').remove(); 
 
     // remove cite error 
 
     blurbt.find('.mw-ext-cite-error').remove(); 
 
     var pOnly = $(blurbt).find('p').text(); 
 
} 
 

 
function firstWiki() { 
 
    var titolo = $("#headingWiki_0 h3 span").text(); 
 
    titolo = encodeURIComponent(titolo); 
 
    $.ajax({ 
 
    url: "https://it.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page=" + titolo + "&callback=?", 
 
    contentType: "application/json; charset=utf-8", 
 
    dataType: "jsonp", 
 
    success: onSuccess 
 
    }); 
 
}
textarea { 
 
    width: 100%; 
 
    height: 200px; 
 
} 
 

 
input[type=checkbox] { 
 
    display: none; 
 
} 
 

 
input[type=checkbox] + label { 
 
    background: #999; 
 
    display: inline-block; 
 
    padding: 0; 
 
} 
 

 
input[type=checkbox]:checked + label { 
 
    border: 10px solid grey; 
 
    padding: 0; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="headingWiki_0"><h3><span>Impero romano</span></h3></div> 
 
<button id="wiki"> 
 
Load 
 
</button> 
 
<textarea id="usp-custom-4"></textarea>

arbeitet

$("#wiki").on("click", function(){ 
 
\t firstWiki(); 
 
}); 
 

 

 
function onSuccess(data){ 
 
     var markupt = data.parse.text["*"]; 
 
     console.log(markupt); 
 
     var blurbt = $('<div></div>').html(markupt); 
 
     blurbt.find(".mw-editsection, #toc, .noprint, .thumb, img, table").remove(); 
 
     // remove links as they will not work 
 
     blurbt.find('a').each(function() { $(this).replaceWith($(this).html()); }); 
 
     // remove any references 
 
     blurbt.find('sup').remove(); 
 
     // remove cite error 
 
     blurbt.find('.mw-ext-cite-error').remove(); 
 
     var pOnly = $(blurbt).find('p').text(); 
 
     $('#usp-custom-4').text(pOnly); 
 
} 
 

 
function firstWiki() { 
 
    var titolo = $("#headingWiki_0 h3 span").text(); 
 
    titolo = encodeURIComponent(titolo); 
 
    $.ajax({ 
 
    url: "https://it.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page=" + titolo + "&callback=?", 
 
    contentType: "application/json; charset=utf-8", 
 
    dataType: "jsonp", 
 
    success: onSuccess 
 
    }); 
 
}
textarea { 
 
    width: 100%; 
 
    height: 200px; 
 
} 
 

 
input[type=checkbox] { 
 
    display: none; 
 
} 
 

 
input[type=checkbox] + label { 
 
    background: #999; 
 
    display: inline-block; 
 
    padding: 0; 
 
} 
 

 
input[type=checkbox]:checked + label { 
 
    border: 10px solid grey; 
 
    padding: 0; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="headingWiki_0"><h3><span>Impero romano</span></h3></div> 
 
<button id="wiki"> 
 
Load 
 
</button> 
 
<textarea id="usp-custom-4"></textarea>

0

Nur entferne das &callback=? von der URL. Es sollte wie folgt aussehen:

https://it.wikipedia.org/w/api.php?action=parse&format=json&prop=text&page=titolo

Die URL, die Sie verwendet nicht eine JSON zurückgeben, aber das man tun sollte.

Wenn Sie CORS Probleme haben, als Sie JSONP Anfragen machen sollte:

function onSuccess(data){ 
    var markupt = data.parse.text["*"]; 
    var blurbt = $('<div></div>').html(markup); 
    blurbt.find(".mw-editsection, #toc, .noprint, .thumb, img, table").remove(); 
    // remove links as they will not work 
    blurbt.find('a').each(function() { $(this).replaceWith($(this).html()); }); 
    // remove any references 
    blurbt.find('sup').remove(); 
    // remove cite error 
    blurbt.find('.mw-ext-cite-error').remove(); 
    $('#usp-custom-4').val($(blurbt).find('p')); 
} 

$.ajax({ 
    url: "https://it.wikipedia.org/w/api.php?action=parse&format=json&prop=text&page=titolo", 
    dataType: "jsonp", 
    jsonpCallback: "onSuccess" 
}) 
+0

wo geben Sie den Seitennamen dann? Und das wird dazu führen access-control-allow-origin error btw –

+0

Sie können es als Abfrageparameter übergeben. Ich aktualisierte die Antwort. – caisah

+0

nichts, es funktioniert nicht –

Verwandte Themen