【发布时间】:2022-10-23 17:23:17
【问题描述】:
这是我第一次使用axios进行查询...但是现在我不知道进一步了,希望有人能给我建议。
为了使用 React Native 开发字典应用程序,我想像这样查询维基词典
let url = "https://en.wiktionary.org/w/api.php?format=json&action=query&titles={word}&rvprop=content&prop=revisions&redirects=1&callback=?".replace("{word}", word);
...
axios({
method: 'get',
url: url,
}).then((response) => {
var results = {
title: "",
definitions: [],
examples: []
}
....
let data = response.data;
...
这个查询本身有效......现在我想根据我的目的调整它:wiktionary-parser。
问题出现在这里:
if(!data || !data.query || !data.query.pages || data.query.pages[-1]) {
return callback({});
}
它说
TypeError: Cannot read property 'pages' of undefined
我的查询中的数据的组织方式必须不同于上面提到的维基词典解析器的“$.getJSON...”查询接收到的数据...
但是怎么做?
我试着和
JSON.stringify(response.data)
和
JSON.parse(response.data)
我究竟做错了什么?有什么建议吗?
提前谢谢你,弗兰克
查询的完整代码是
function getENWiktionaryInfo(word, wordLanguage, callback) {
// getJSON("https://en.wiktionary.org/w/api.php?format=json&action=query&titles={word}&rvprop=content&prop=revisions&redirects=1&callback=?".replace("{word}", word), function (data) {
// $.getJSON("https://en.wiktionary.org/wiki/abdico#Latin", function (data) {
let url = "https://en.wiktionary.org/w/api.php?format=json&action=query&titles={word}&rvprop=content&prop=revisions&redirects=1&callback=?".replace("{word}", word);
console.log("getENWiktionaryInfo " + url);
axios({
method: 'get',
url: url,
}).then((response) => {
var results = {
title: "",
definitions: [],
examples: []
}
let data = response.data;
console.log("DATA "+data);
const jsonObj= JSON.stringify(response.data)
//let data = jsonObj;
var title, content;
if (!data || !data.query || !data.query.pages || data.query.pages[-1]) {
return callback({});
}
callback(results);
});
}
(拉丁)单词“res”的纯粹调用是:
https://en.wiktionary.org/w/api.php?format=json&action=query&titles=res&rvprop=content&prop=revisions&redirects=1&callback=?
【问题讨论】:
标签: axios