我有这段代码,它对维基百科进行 AJAX 调用,询问给定查询的结果 (var searchText):
function main() {
$(".btn").click(function() {
$("#iframe").attr('src', 'https://en.wikipedia.org/wiki/Special:Random');
$(".embed-container").css('visibility', 'visible');
});
function wikiAjax (searchURL) {
return $.ajax({
url: searchURL,
jsonp: "callback",
dataType: 'jsonp',
xhrFields: {
withCredentials: true
}
});
}
$(".search-form").submit(function() {
event.preventDefault();
var searchText = $('#search').val();
var searchURL = "https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrsearch=" + searchText + "&gsrlimit=15&prop=extracts&exsentences=3&exintro=&explaintext&exlimit=max&callback=JSON_CALLBACK";
console.log(searchURL);
var wikiResponse = wikiAjax(searchURL);
wikiResponse.done(function(data) {
console.log(data);
}).fail(function(err) {
alert("The call has been rejected");
});
});
}
$(document).ready(main);
但它返回给我一个奇怪的对象:
有人可以帮帮我吗?
最佳答案
你从维基百科得到了正确的回应,检查你的查询参数,特别是这个
var searchText = $('#search').val();
您正在测试哪个值,如果您输入类似“2sasd23sda”的内容,您将获得此对象响应。
关于javascript - 维基百科 AJAX 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36407375/