我在运行时遇到标题中的错误。我正在尝试在运行此
时解析第一段没有任何htmlHTML
<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'));
});
控制台说:
未捕获的TypeError:无法读取属性&#39; text&#39;未定义的
答案 0 :(得分:0)
只需从网址中删除&callback=?
即可。它应该看起来像:
https://it.wikipedia.org/w/api.php?action=parse&format=json&prop=text&page=titolo
您使用的网址不会返回JSON,但是应该这样做。
如果您遇到CORS问题,那么您应该提出JSONP请求:
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"
})
答案 1 :(得分:0)
感谢我从另一个so question获得,这是有效的代码
$("#wiki").on("click", function(){
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§ion=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>
$("#wiki").on("click", function(){
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§ion=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>