如果JQuery中的$.getJSON不提供德语,请将维基百科文本保存为英语

Save Wikipedia text in English if German not available with $.getJSON in JQuery

本文关键字:百科 英语 保存 文本 getJSON 中的 JQuery 德语 如果      更新时间:2023-09-26

我获取特定关键字(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);

如何保持价值?还有没有更好的方法来检查我想要获得的特定内容(本例中的文本)是否在之前可用,这样我就不必发出两个$.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);

在分配文本之前立即调用,因为执行就是这样进行的。如果你把你想用文本做的事情的代码放在回调函数中,你会得到你想要的结果。