【问题标题】:Not getting a json output form axios没有从 axios 获得 json 输出
【发布时间】:2019-09-19 20:14:02
【问题描述】:

我正在使用 axios 从 Wikipedia Api 检索数据。这是我写的代码。

let axiosData = function(){
let searchString = $('#searchString').val();
console.log(searchString);
let Url = "https://en.wikipedia.org/w/api.php?action=opensearch&search="+ searchString + 
"&origin=*&callback=";
axios.get(Url)
 .then(function(res){
     var linkLists = res.data;
     console.log(linkLists);
 })
 .catch(function(){
     console.log("Error")
 });
return false;
}

$('form').submit(axiosData);

我能够在控制台记录它时获得输出。以下是:在这种情况下,我正在搜索 Jon Snow 的名字。如何访问 json?

/**/(["jon snow",["Jon Snow (character)","Jon Snow (journalist)","Jon Snow","John Snow (cricketer)","Jon Snoddy","John Snow","Jon Snodgrass (musician)","Jon Snodgrass","John Snow College, Durham","John Snow, Inc"],["Jon Snow is a fictional character in the A Song of Ice and Fire series of fantasy novels by American author George R. R.","Jonathan George Snow HonFRIBA (born 28 September 1947) is an English journalist and television presenter.","Jon Snow may refer to:","John Augustine Snow (born 13 October 1941) is a retired English cricketer. He played for Sussex and England in the 1960s and 1970s.","Jon Snoddy is an American technology expert who is currently the Advanced Development Studio Executive SVP at Walt Disney Imagineering.","John Snow (15 March 1813 \u2013 16 June 1858) was an English physician and a leader in the  development of  anaesthesia and medical hygiene.","Jon Snodgrass is \"the guy with the glasses from Drag the River\".","Jon Snodgrass is a Panamanian author, born on July 27, 1941 in Col\u00f3n, Panama to John Alphonso and Olivia Jane (Chestnut) Snodgrass.","John Snow College is one of 16 constituent colleges of the University of Durham in England. The College takes its name from the nineteenth-century Yorkshire physician Dr John Snow.","John Snow, Inc. (JSI) is a public health research and consulting firm in the United States and around the world."],["https://en.wikipedia.org/wiki/Jon_Snow_(character)","https://en.wikipedia.org/wiki/Jon_Snow_(journalist)","https://en.wikipedia.org/wiki/Jon_Snow","https://en.wikipedia.org/wiki/John_Snow_(cricketer)","https://en.wikipedia.org/wiki/Jon_Snoddy","https://en.wikipedia.org/wiki/John_Snow","https://en.wikipedia.org/wiki/Jon_Snodgrass_(musician)","https://en.wikipedia.org/wiki/Jon_Snodgrass","https://en.wikipedia.org/wiki/John_Snow_College,_Durham","https://en.wikipedia.org/wiki/John_Snow,_Inc"]])

【问题讨论】:

    标签: json api axios mediawiki


    【解决方案1】:

    在 json 的开头有 '/**/(' 和结尾的 ')' 字符,这些字符被子字符串剪切。解析后。

    let Url = "https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=jon%20snow&origin=*&callback=";
    axios.get(Url)
    .then(function(res){
     console.log(res);
     var linkLists = JSON.parse(res.data.substring(5, res.data.length-1));
     console.log(linkLists)
     })
     .catch(function(){
       console.log("Error...")
     });
    

    【讨论】:

      【解决方案2】:

      如果 res.data 是您的 http 请求的 JSON 响应,您可以使用 JSON.parse() 函数将其解析为 JSON

      var linkJSON = JSON.parse(res.data)
      

      这将为您提供 linkJSON 对象中的 json 数据。

      【讨论】:

      • 不幸的是,我遇到了一个错误。我认为 axios 无论如何都应该解析 JSON。我使用上面的 URL 来绕过 CORS。不幸的是,如果我使用以下 "https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search="+ searchString ; 我会得到 CORS,我认为 axios 不能处理 JSONP。