Error: Cannot read property 'text' of undefined
Error: Cannot read property 'text' of undefined
我在 运行 这个时候标题有误。我试图在没有任何 html 而 运行 this
的情况下解析第一段
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'));
});
控制台说:
Uncaught TypeError: Cannot read property 'text' of undefined
只需从 url 中删除 &callback=?
。它应该看起来像:
https://it.wikipedia.org/w/api.php?action=parse&format=json&prop=text&page=titolo
您使用的 url 没有 return 一个 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"
})
多亏了我从另一个 那里得到的,这是有效的代码
$("#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>
我在 运行 这个时候标题有误。我试图在没有任何 html 而 运行 this
的情况下解析第一段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'));
});
控制台说:
Uncaught TypeError: Cannot read property 'text' of undefined
只需从 url 中删除 &callback=?
。它应该看起来像:
https://it.wikipedia.org/w/api.php?action=parse&format=json&prop=text&page=titolo
您使用的 url 没有 return 一个 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"
})
多亏了我从另一个
$("#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>