jQueryAjax在使用WikipediaAPI的JSON解析时抛出错误
我正在尝试使用WikipediaAPI解析JSON数据,URL正确返回JSON数据,我想要搜索符号的片段部分。。。但是遇到了错误。 我已经添加了代码供您考虑,并且还学习了一些JSON解析教程,但是这个错误似乎不会消失 JSON数据:jQueryAjax在使用WikipediaAPI的JSON解析时抛出错误,jquery,ajax,Jquery,Ajax,我正在尝试使用WikipediaAPI解析JSON数据,URL正确返回JSON数据,我想要搜索符号的片段部分。。。但是遇到了错误。 我已经添加了代码供您考虑,并且还学习了一些JSON解析教程,但是这个错误似乎不会消失 JSON数据: { "batchcomplete": "", "continue": { "sroffset": 10, "continue": "-||" }, "query": { "searchi
{
"batchcomplete": "",
"continue": {
"sroffset": 10,
"continue": "-||"
},
"query": {
"searchinfo": {
"totalhits": 1773
},
"search": [
{
"ns": 0,
"title": "Avengers: Infinity War",
"pageid": 44240443,
"snippet": "<span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> War is a 2018 American superhero film based on the Marvel Comics superhero team the <span class=\"searchmatch\">Avengers</span>, produced by Marvel Studios and distributed"
},
{
"ns": 0,
"title": "Avengers: Endgame",
"pageid": 44254295,
"snippet": "<span class=\"searchmatch\">Avengers</span>: Endgame is a 2019 American superhero film based on the Marvel Comics superhero team the <span class=\"searchmatch\">Avengers</span>, produced by Marvel Studios and distributed"
},
{
"ns": 0,
"title": "Avengers Infinity",
"pageid": 3511035,
"snippet": "<span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> is a four-issue American comic book limited series published from September to December 2000 by Marvel Comics. It was written by Roger"
},
{
"ns": 0,
"title": "Production of Avengers: Infinity War and Avengers: Endgame",
"pageid": 46208997,
"snippet": "<span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> War and <span class=\"searchmatch\">Avengers</span>: Endgame are American superhero films based on the Marvel Comics superhero team the <span class=\"searchmatch\">Avengers</span>, produced by Marvel Studios"
},
{
"ns": 0,
"title": "List of Marvel Cinematic Universe films",
"pageid": 42163310,
"snippet": "Captain America: Civil War, The <span class=\"searchmatch\">Avengers</span>, and <span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> War), and the final day showing Iron Man and The <span class=\"searchmatch\">Avengers</span> as chosen by the fans via a Twitter"
},
{
"ns": 0,
"title": "The Infinity Gauntlet",
"pageid": 515090,
"snippet": "third and fourth <span class=\"searchmatch\">Avengers</span> films were revealed to be <span class=\"searchmatch\">Infinity</span> War - Part I and <span class=\"searchmatch\">Infinity</span> War - Part II. As the 2018 release of <span class=\"searchmatch\">Infinity</span> War neared, several"
},
{
"ns": 0,
"title": "Avengers: Infinity War (soundtrack)",
"pageid": 57256096,
"snippet": "<span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> War (Original Motion Picture Soundtrack) is the film score for the Marvel Studios film <span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> War by Alan Silvestri. Hollywood"
},
{
"ns": 0,
"title": "Infinity Gems",
"pageid": 606571,
"snippet": "mid-credits scene of <span class=\"searchmatch\">Avengers</span>: Age of Ultron revealed Thanos had acquired a left-handed gauntlet (the real one). <span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> War elaborates on this"
},
{
"ns": 0,
"title": "Infinity (comic book)",
"pageid": 38382468,
"snippet": "Vol 2013 <span class=\"searchmatch\">Infinity</span> <span class=\"searchmatch\">Avengers</span> Vol 5 #16 <span class=\"searchmatch\">Avengers</span> Vol. 5 #17 New <span class=\"searchmatch\">Avengers</span> Vol. 3 #8 <span class=\"searchmatch\">Infinity</span> #1 New <span class=\"searchmatch\">Avengers</span> Vol. 3 #9 <span class=\"searchmatch\">Infinity</span> #2 New <span class=\"searchmatch\">Avengers</span> Vol. 3 #10"
},
{
"ns": 0,
"title": "Avengers: Age of Ultron",
"pageid": 36484254,
"snippet": "<span class=\"searchmatch\">Avengers</span>: Age of Ultron is a 2015 American superhero film based on the Marvel Comics superhero team the <span class=\"searchmatch\">Avengers</span>, produced by Marvel Studios and distributed"
}
]
}
}
Uncaught SyntaxError: Unexpected token u in JSON at position 0
at Function.parse [as parseJSON] (<anonymous>)
at Object.success (wikipedia-fetch-api.html:18)
at c (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at l (jquery.min.js:2)
at HTMLScriptElement.i (jquery.min.js:2)
at HTMLScriptElement.dispatch (jquery.min.js:2)
at HTMLScriptElement.v.handle (jquery.min.js:2)
{
“批处理完成”:“,
“继续”:{
“sroffset”:10,
“继续”:“-||”
},
“查询”:{
“搜索信息”:{
“总点击数”:1773
},
“搜索”:[
{
“ns”:0,
“标题”:“复仇者:无限战争”,
“pageid”:44240443,
“片段”:“复仇者:无限战争”是一部2018年美国超级英雄电影,基于惊奇漫画超级英雄团队《复仇者》,由惊奇漫画工作室制作并发行
},
{
“ns”:0,
“标题”:“复仇者:终局”,
“pageid”:44254295,
“片段”:“复仇者:结局”是一部2019年美国超级英雄电影,由惊奇漫画超级英雄团队《复仇者》改编,由惊奇漫画工作室制作并发行
},
{
“ns”:0,
“标题”:“复仇者无限”,
“pageid”:3511035,
《片段》:《复仇者:无限》是由漫威漫画公司于2000年9月至12月出版的四期美国漫画系列,由罗杰撰写
},
{
“ns”:0,
“标题”:“复仇者的制作:无限战争和复仇者:结局”,
“pageid”:46208997,
“片段”:“复仇者:无限战争和复仇者:结局”是美国超级英雄电影,由漫威漫画公司的超级英雄团队《复仇者》改编,由漫威工作室制作
},
{
“ns”:0,
“标题”:“惊奇漫画电影宇宙电影列表”,
“pageid”:42163310,
“片段”:“美国队长:内战,复仇者和复仇者:无限战争”,以及粉丝通过推特选出的《钢铁侠和复仇者》的最后一天
},
{
“ns”:0,
“标题”:“无限挑战”,
“pageid”:515090,
“片段”:“第三部和第四部复仇者电影被披露为《无限战争》第一部分和《无限战争》第二部分。随着2018年《无限战争》的发行临近,有几部”
},
{
“ns”:0,
“标题”:“复仇者:无限战争(配乐)”,
“pageid”:57256096,
“片段”:“复仇者:无限战争(原电影配乐)是惊奇电影公司艾伦·西尔维斯特里(Alan Silvestri)的电影《复仇者:无限战争》的电影配乐。好莱坞”
},
{
“ns”:0,
“标题”:“无限宝石”,
“pageid”:606571,
“片段:”《复仇者:奥特龙时代》中的场景显示塔诺斯获得了一个左手手套(真实的)。复仇者:无限战争对此进行了详细阐述”
},
{
“ns”:0,
“标题”:“无限(漫画书)”,
“pageid”:382468,
“片段”:“2013年《无限复仇者》第5卷#16《复仇者》第5卷#17《新复仇者》第3卷#8《无限》#1《新复仇者》第3卷#9《无限》#2《新复仇者》第3卷#10”
},
{
“ns”:0,
“标题”:“复仇者:奥创时代”,
“pageid”:36484254,
“片段”:“复仇者:奥创时代”是一部2015年美国超级英雄电影,由惊奇漫画超级英雄团队《复仇者》改编,由惊奇漫画工作室制作并发行
}
]
}
}
代码
<!Doctype html>
<html>
<head>
<title>Wikipedia API Posts</title>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js'></script>
<script type="text/javascript">
$(document).ready(function() {
var searchp = 'AVENGER INFINITY WAR';
$.ajax({
method: "GET",
dataType: "jsonP",
url: 'https://en.wikipedia.org/w/api.php?action=query&list=search&srwhat=text&format=json&srprop=snippet&srsearch='+searchp,
success: function(response) {
var s = $.parseJSON(response.responseText);
console.log(s);
$("#WikipediaAPI").html(s.search[snippet]);
}
});
});
</script>
</head>
<body>
<div id="WikipediaAPI"></div>
</body>
</html>
维基百科API帖子
$(文档).ready(函数(){
var searchp=‘复仇者无限战争’;
$.ajax({
方法:“获取”,
数据类型:“jsonP”,
网址:'https://en.wikipedia.org/w/api.php?action=query&list=search&srwhat=text&format=json&srprop=snippet&srsearch=“+searchp,
成功:功能(响应){
var s=$.parseJSON(response.responseText);
控制台日志;
$(“#维基百科API”).html(s.search[snippet]);
}
});
});
返回此错误:
{
"batchcomplete": "",
"continue": {
"sroffset": 10,
"continue": "-||"
},
"query": {
"searchinfo": {
"totalhits": 1773
},
"search": [
{
"ns": 0,
"title": "Avengers: Infinity War",
"pageid": 44240443,
"snippet": "<span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> War is a 2018 American superhero film based on the Marvel Comics superhero team the <span class=\"searchmatch\">Avengers</span>, produced by Marvel Studios and distributed"
},
{
"ns": 0,
"title": "Avengers: Endgame",
"pageid": 44254295,
"snippet": "<span class=\"searchmatch\">Avengers</span>: Endgame is a 2019 American superhero film based on the Marvel Comics superhero team the <span class=\"searchmatch\">Avengers</span>, produced by Marvel Studios and distributed"
},
{
"ns": 0,
"title": "Avengers Infinity",
"pageid": 3511035,
"snippet": "<span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> is a four-issue American comic book limited series published from September to December 2000 by Marvel Comics. It was written by Roger"
},
{
"ns": 0,
"title": "Production of Avengers: Infinity War and Avengers: Endgame",
"pageid": 46208997,
"snippet": "<span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> War and <span class=\"searchmatch\">Avengers</span>: Endgame are American superhero films based on the Marvel Comics superhero team the <span class=\"searchmatch\">Avengers</span>, produced by Marvel Studios"
},
{
"ns": 0,
"title": "List of Marvel Cinematic Universe films",
"pageid": 42163310,
"snippet": "Captain America: Civil War, The <span class=\"searchmatch\">Avengers</span>, and <span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> War), and the final day showing Iron Man and The <span class=\"searchmatch\">Avengers</span> as chosen by the fans via a Twitter"
},
{
"ns": 0,
"title": "The Infinity Gauntlet",
"pageid": 515090,
"snippet": "third and fourth <span class=\"searchmatch\">Avengers</span> films were revealed to be <span class=\"searchmatch\">Infinity</span> War - Part I and <span class=\"searchmatch\">Infinity</span> War - Part II. As the 2018 release of <span class=\"searchmatch\">Infinity</span> War neared, several"
},
{
"ns": 0,
"title": "Avengers: Infinity War (soundtrack)",
"pageid": 57256096,
"snippet": "<span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> War (Original Motion Picture Soundtrack) is the film score for the Marvel Studios film <span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> War by Alan Silvestri. Hollywood"
},
{
"ns": 0,
"title": "Infinity Gems",
"pageid": 606571,
"snippet": "mid-credits scene of <span class=\"searchmatch\">Avengers</span>: Age of Ultron revealed Thanos had acquired a left-handed gauntlet (the real one). <span class=\"searchmatch\">Avengers</span>: <span class=\"searchmatch\">Infinity</span> War elaborates on this"
},
{
"ns": 0,
"title": "Infinity (comic book)",
"pageid": 38382468,
"snippet": "Vol 2013 <span class=\"searchmatch\">Infinity</span> <span class=\"searchmatch\">Avengers</span> Vol 5 #16 <span class=\"searchmatch\">Avengers</span> Vol. 5 #17 New <span class=\"searchmatch\">Avengers</span> Vol. 3 #8 <span class=\"searchmatch\">Infinity</span> #1 New <span class=\"searchmatch\">Avengers</span> Vol. 3 #9 <span class=\"searchmatch\">Infinity</span> #2 New <span class=\"searchmatch\">Avengers</span> Vol. 3 #10"
},
{
"ns": 0,
"title": "Avengers: Age of Ultron",
"pageid": 36484254,
"snippet": "<span class=\"searchmatch\">Avengers</span>: Age of Ultron is a 2015 American superhero film based on the Marvel Comics superhero team the <span class=\"searchmatch\">Avengers</span>, produced by Marvel Studios and distributed"
}
]
}
}
Uncaught SyntaxError: Unexpected token u in JSON at position 0
at Function.parse [as parseJSON] (<anonymous>)
at Object.success (wikipedia-fetch-api.html:18)
at c (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at l (jquery.min.js:2)
at HTMLScriptElement.i (jquery.min.js:2)
at HTMLScriptElement.dispatch (jquery.min.js:2)
at HTMLScriptElement.v.handle (jquery.min.js:2)
Uncaught SyntaxError:JSON中位于位置0的意外标记u
在Function.parse[as parseJSON]()
在Object.success(wikipediafetchapi.html:18)
at c(jquery.min.js:2)
在Object.fireWith[as resolveWith](jquery.min.js:2)
在l(jquery.min.js:2)
在HTMLScriptElement.i(jquery.min.js:2)
在HTMLScriptElement.dispatch(jquery.min.js:2)
位于HTMLScriptElement.v.handle(jquery.min.js:2)
维基百科API帖子
$(文档).ready(函数(){
var searchp=‘复仇者无限战争’;
$.ajax({
方法:“获取”,
数据类型:“jsonP”,
网址:'https://en.wikipedia.org/w/api.php?action=query&list=search&srwhat=text&format=json&srprop=snippet&srsearch=“+searchp,
成功:功能(响应){
var s=response.query.search
var=”;
控制台日志
$。每个功能(索引,el){
snippetEl+=el.snippet;
snippetEl+='
';
})
$(“#维基百科API”).html(snippetEl);
}
});
});
console.log(response)
并与我们共享它是什么。@Taplar按照您的建议更改为console.log(response)
,但返回相同的错误。没有响应的输出。错误,如果响应的控制台日志记录没有记录任何内容,那么您会遇到更大的问题。在success方法中,它应该记录一个字符串或一个obj