这是我第一次使用axios查询..。但是现在我不知道了,我希望有人能给我一个建议。
为了开发一个带有Reacti原住民的字典应用程序,我想像这样查询wiktionary
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-解析器。
问题发生在这里:
if(!data || !data.query || !data.query.pages || data.query.pages[-1]) {
return callback({});
}上面写着
TypeError: Cannot read property 'pages' of undefined来自我的查询的数据的组织方式必须不同于上面提到的这个“$.getJSON.”-query所接收的数据.
但是怎么做呢?
我试着和
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=?发布于 2022-10-23 09:47:12
你可以像这样使用它,这样会更好
async function getENWiktionaryInfo(word, wordLanguage, callback) {
try {
console.log("getENWiktionaryInfo " + url)
const response = await axios.get("https://en.wiktionary.org/w/api.php", {
params: {
format: "json",
action: "query",
titles: word,
rvprop: "content",
prop: "revisions",
redirects: "1",
callback: "?",
},
})
const data = response.data
if (data?.query?.pages?.length) {
throw new Error("Something went wrong")
}
return data
} catch (error) {
console.log("error", error)
}
}发布于 2022-10-23 09:23:17
我希望我没有因为我的问题而打扰你.在这里谈过话,但所有的评论都消失了?
总之..。Axios和其他查询(如fetch(.))之间一定有区别。或getJSON(.)-见在这里讨论和这里
我试着用谷歌铬的控制台重放这个.但我不能在那里使用公理..。我试着为这个设置Codesandbox.io,但是
Access to XMLHttpRequest at 'https://en.wiktionary.org/w/api.php?format=json&action=query&titles=res&rvprop=content&prop=revisions&redirects=1&callback=?' from origin 'https://k62hb0.csb.app' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.我的最后一个想法是:替换这个查询
https://en.wiktionary.org/w/api.php?format=json&action=query&titles=res&rvprop=content&prop=revisions&redirects=1&callback=?通过这个
https://en.wiktionary.org/w/index.php?title=res&callback=?&action=raw&origin=*..。而且成功了..。太令人吃惊了。
这个不同的查询似乎返回原始标记文本(不幸的是,我无法记住我在哪里找到了它),但这正是我所需要的。将来,我的任务是学习更多关于axios的javascript细节等等。
我想解析这个文本并提取我需要的信息--使用上面提到的这个wiktionary解析器作为模板。
感谢所有想要帮助这里的人!
亲切的问候,弗兰克
https://stackoverflow.com/questions/74153774
复制相似问题