繁体   English   中英

错误:无法读取未定义的属性“文本”

[英]Error: Cannot read property 'text' of undefined

运行此程序时标题出现错误。 我正在尝试运行第一段而没有任何html解析

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'));

  });

控制台说:

未捕获的TypeError:无法读取未定义的属性“文本”

只需删除&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"
})

多亏了我从另一个问题得到的答案 ,这是有效的代码

 $("#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&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> 

 $("#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&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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM