Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

javascript - 使用Wikipedia的opensearch api时,井号标记不会填充数据列表。 为什么?(When using Wikipedia's opensearch api, the hashtag symbol will not populate the datalist. Why?)

$.ajax({
    url: "https://en.wikipedia.org/w/api.php",
    dataType: "jsonp",
    data: {
        'action': "opensearch",
        'format': "json",
        'search': search
    },
    success: function (data) {
        var suggestions = '';
        data[1].forEach(d => {
            suggestions += `<option value="${d}">`;
        });
        console.log(data);
        $('#searchList').html(suggestions);
        console.log(suggestions);
    }
});

Difference between hashtag symbol from wikipedia search api and local server(来自Wikipedia搜索API的主题标签符号和本地服务器之间的区别)

In other words the datalist populates just fine normally with other characters including @ or $, but when using # it will not drop down and show the suggestions even though the datalist is populated with the correct items when inspecting the element.(换句话说,数据列表通常可以正常填充其他字符,包括@或$,但是使用#时,即使检查元素时数据列表中填充了正确的项目,它也不会下拉并显示建议。) edit: by localhost I meant I changed the endpoint to my server so I could see what would happen if I sent back the option list with a hashtag.(编辑:通过本地主机,我的意思是我将端点更改为服务器,因此可以看到如果将带有井号的选项列表发送回去会发生什么。) It worked fine and I noticed the different font between the hashtags sent by my server vs wiki's opensearch.(它工作正常,我注意到服务器发送的主题标签与Wiki的opensearch之间的字体不同。) edit: The search variable comes from the input box like this-(编辑:搜索变量来自像这样的输入框-) <form class="pSearch form-inline" method="post" action=""> <input class="pSearch" type="text" id="searchTerm" name="searchTerms" placeholder="Search" aria-label="Search" list="searchList" autocomplete="off" spellcheck="true"> <datalist id="searchList"></datalist> I listen for input with the keyup function-(我用keyup功能收听输入-) $('#searchTerm').keyup(function (e) { var search = $(this).val(); Then I send the variable search to the opensearch api-(然后,将变量搜索发送到opensearch api-) $.ajax({...}) } Hardcoding the hashtag instead of using a variable works fine- "#".(硬编码主题标签而不是使用变量可以正常使用“#”。) I tried JSON.stringify(search), but it made no difference.(我尝试了JSON.stringify(search),但是没有区别。)   ask by jmoney translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that Wikipedia article titles cannot contain a # character .(问题在于Wikipedia文章标题不能包含#字符 。)

Apparently some people got creative and instead used the FULLWIDTH NUMBER SIGN instead to circumvent this limitation.(显然,有些人很有创造力,而是使用全数字符号来规避此限制。) Correspondingly, the normal search would find only a single accurate result for # , but the Opensearch API that you are using appears to do some unicode-aware fuzzy matching and returns pages beginning with .(相应地, 普通搜索找到#的单个准确结果 ,但是您正在使用的Opensearch API似乎进行了一些unicode感知的模糊匹配,并返回以开头的页面。) The <datalist> won't consider those however.(<datalist>不会考虑这些。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...