如果德语无法使用 JQuery 中的 $.getJSON,则将维基百科文本保存为英语
Save Wikipedia text in English if German not available with $.getJSON in JQuery
我获取了特定关键字 (var title
) 的德语文本,然后将其输出为 html。这工作正常,但现在我想在德文文本不可用时加载英文文本。这也适用于我的代码:
var length = 500;
var title = $('#title').attr('data-title');
var lang = 'de';
var url = 'https://' + lang + '.wikipedia.org/w/api.php?format=json&action=query' +
'&prop=extracts&exintro=&explaintext=&titles=' + title + '&redirects=0';
$.getJSON("http://query.yahooapis.com/v1/public/yql",
{
q: "select * from json where url=\"" + url + "\"",
format: "json"
},
function (data) {
$.each(data.query.results.json.query.pages, function (key, val) {
var text = val['extract'];
console.log('lang-' + lang + '-text: ' + text);
if (text) {
text = text.replace('Siehe auch:', '');
} else if (!text && lang != 'en') {
var url = 'https://en.wikipedia.org/w/api.php?format=json&action=query' +
'&prop=extracts&exintro=&explaintext=&titles=' + title + '&redirects=0';
$.getJSON("http://query.yahooapis.com/v1/public/yql",
{
q: "select * from json where url=\"" + url + "\"",
format: "json"
},
function (data) {
$.each(data.query.results.json.query.pages, function (key, val) {
text = val['extract'];
console.log('lang-en-text: ' + text);
});
});
}
console.log('lang-end-text: ' + text);
if (text) {
text = text.length > length ? text.substring(0, length - 3) + '...' : text;
$('#text').html(text);
} else {
setTimeout(function () {
$('#text').html('<?= __('EMPTY'); ?>');
}, 1000);
}
console.log(data);
});
});
但是第二个$.getJSON关闭后,text
又是空的。这意味着
console.log('lang-en-text: ' + text);
正在工作并在控制台中输出正确的英文文本,但是在关闭 $.getJSON 之后变量 text
不再具有任何值,我可以通过控制台中的输出确认:
console.log('lang-end-text: ' + text);
如何保值?另外有没有更好的方法来检查我想要获取的特定内容(本例中的文本)是否可用BEFORE,所以我不必制作两个$.getJSON
要求?或者我的方法是否正确?
编辑:现在可以使用了!
感谢 moopet
,我找到了解决方案,并使用 .done
和一个名为 .setText
的新函数来设置文本。也许这对其他人也有帮助,因为这个问题似乎得到了很多支持。这是我的代码:
var length = 500;
var title = $('#title').attr('data-title');
var lang = 'de';
var url = 'https://' + lang + '.wikipedia.org/w/api.php?format=json&action=query' +
'&prop=extracts&exintro=&explaintext=&titles=' + title + '&redirects=0';
$.getJSON("http://query.yahooapis.com/v1/public/yql",
{
q: "select * from json where url=\"" + url + "\"",
format: "json"
},
function (data) {
$.each(data.query.results.json.query.pages, function (key, val) {
var text = val['extract'];
console.log('lang-' + lang + '-text: ' + text);
if (text) {
text = text.replace('Siehe auch:', '');
} else if (!text && lang != 'en') {
var url = 'https://en.wikipedia.org/w/api.php?format=json&action=query' +
'&prop=extracts&exintro=&explaintext=&titles=' + title + '&redirects=0';
$.getJSON("http://query.yahooapis.com/v1/public/yql",
{
q: "select * from json where url=\"" + url + "\"",
format: "json"
},
function (data) {
$.each(data.query.results.json.query.pages, function (key, val) {
text = val['extract'];
console.log('lang-en-text: ' + text);
});
}).done(function() {
setText(text);
});
}
console.log(data);
});
}).done(function() {
setText(text);
});
function setText(text) {
if (text) {
text = text.length > length ? text.substring(0, length - 3) + '...' : text;
$('#text').html(text);
} else {
$('#text').html('Text not available.');
}
}
您 运行 与异步 javascript 调用冲突。
您的成功回调:
function (data) {
$.each(data.query.results.json.query.pages, function (key, val) {
text = val['extract'];
console.log('lang-en-text: ' + text);
});
});
被称为异步。换句话说,它被推迟到 HTTP 请求完成。
你的
console.log('lang-end-text: ' + text);
在分配文本之前立即调用,因为这是执行的方式。如果你把你想用文本做的事情的代码放在回调函数里,你会得到你想要的结果。
我获取了特定关键字 (var title
) 的德语文本,然后将其输出为 html。这工作正常,但现在我想在德文文本不可用时加载英文文本。这也适用于我的代码:
var length = 500;
var title = $('#title').attr('data-title');
var lang = 'de';
var url = 'https://' + lang + '.wikipedia.org/w/api.php?format=json&action=query' +
'&prop=extracts&exintro=&explaintext=&titles=' + title + '&redirects=0';
$.getJSON("http://query.yahooapis.com/v1/public/yql",
{
q: "select * from json where url=\"" + url + "\"",
format: "json"
},
function (data) {
$.each(data.query.results.json.query.pages, function (key, val) {
var text = val['extract'];
console.log('lang-' + lang + '-text: ' + text);
if (text) {
text = text.replace('Siehe auch:', '');
} else if (!text && lang != 'en') {
var url = 'https://en.wikipedia.org/w/api.php?format=json&action=query' +
'&prop=extracts&exintro=&explaintext=&titles=' + title + '&redirects=0';
$.getJSON("http://query.yahooapis.com/v1/public/yql",
{
q: "select * from json where url=\"" + url + "\"",
format: "json"
},
function (data) {
$.each(data.query.results.json.query.pages, function (key, val) {
text = val['extract'];
console.log('lang-en-text: ' + text);
});
});
}
console.log('lang-end-text: ' + text);
if (text) {
text = text.length > length ? text.substring(0, length - 3) + '...' : text;
$('#text').html(text);
} else {
setTimeout(function () {
$('#text').html('<?= __('EMPTY'); ?>');
}, 1000);
}
console.log(data);
});
});
但是第二个$.getJSON关闭后,text
又是空的。这意味着
console.log('lang-en-text: ' + text);
正在工作并在控制台中输出正确的英文文本,但是在关闭 $.getJSON 之后变量 text
不再具有任何值,我可以通过控制台中的输出确认:
console.log('lang-end-text: ' + text);
如何保值?另外有没有更好的方法来检查我想要获取的特定内容(本例中的文本)是否可用BEFORE,所以我不必制作两个$.getJSON
要求?或者我的方法是否正确?
编辑:现在可以使用了!
感谢 moopet
,我找到了解决方案,并使用 .done
和一个名为 .setText
的新函数来设置文本。也许这对其他人也有帮助,因为这个问题似乎得到了很多支持。这是我的代码:
var length = 500;
var title = $('#title').attr('data-title');
var lang = 'de';
var url = 'https://' + lang + '.wikipedia.org/w/api.php?format=json&action=query' +
'&prop=extracts&exintro=&explaintext=&titles=' + title + '&redirects=0';
$.getJSON("http://query.yahooapis.com/v1/public/yql",
{
q: "select * from json where url=\"" + url + "\"",
format: "json"
},
function (data) {
$.each(data.query.results.json.query.pages, function (key, val) {
var text = val['extract'];
console.log('lang-' + lang + '-text: ' + text);
if (text) {
text = text.replace('Siehe auch:', '');
} else if (!text && lang != 'en') {
var url = 'https://en.wikipedia.org/w/api.php?format=json&action=query' +
'&prop=extracts&exintro=&explaintext=&titles=' + title + '&redirects=0';
$.getJSON("http://query.yahooapis.com/v1/public/yql",
{
q: "select * from json where url=\"" + url + "\"",
format: "json"
},
function (data) {
$.each(data.query.results.json.query.pages, function (key, val) {
text = val['extract'];
console.log('lang-en-text: ' + text);
});
}).done(function() {
setText(text);
});
}
console.log(data);
});
}).done(function() {
setText(text);
});
function setText(text) {
if (text) {
text = text.length > length ? text.substring(0, length - 3) + '...' : text;
$('#text').html(text);
} else {
$('#text').html('Text not available.');
}
}
您 运行 与异步 javascript 调用冲突。
您的成功回调:
function (data) {
$.each(data.query.results.json.query.pages, function (key, val) {
text = val['extract'];
console.log('lang-en-text: ' + text);
});
});
被称为异步。换句话说,它被推迟到 HTTP 请求完成。
你的
console.log('lang-end-text: ' + text);
在分配文本之前立即调用,因为这是执行的方式。如果你把你想用文本做的事情的代码放在回调函数里,你会得到你想要的结果。