Javascript .getJSON-won';即使URL有效,也无法成功

Javascript .getJSON-won';即使URL有效,也无法成功,javascript,jquery,getjson,Javascript,Jquery,Getjson,HTML 维基百科查看器 Javascript <body> <header> <div class="container-fluid"> <h1>Wikipedia Viewer</h1> <form id="userInput"> <input id="query" type="text" name="use

HTML


维基百科查看器
Javascript

<body>
    <header>
        <div class="container-fluid">
            <h1>Wikipedia Viewer</h1>
            <form id="userInput">
                <input id="query" type="text" name="userInput" value="" placeholder="Search">
                <input id="submitBtn" type="submit" name="send" value="Click!">
            </form>
            <a id="randomArticle" href="https://en.wikipedia.org/wiki/Special:Random"><i class="fa fa-random fa-2x" aria-hidden="true"></i></a>
        </div>
    </header>
    <div class="results">
         </div>
</body>

</html>
var url=”https://en.wikipedia.org/w/api.php?action=opensearch&datatype=json&callback=?&search=";
var userQuery=“”;
var html=“”;
$(文档).ready(函数(){
$(“#用户输入”).submit(函数(e){
userQuery=document.getElementById(“查询”).value;
$.getJSON(url+userQuery,函数(数据){
对于(var i=0;i

当我使用.getJSON时,它不会成功,我已经测试了URL,JSON出现在浏览器中,但是使用Jquery的请求不会起作用。

您可以记录成功和失败方法吗。可能存在跨源问题,如果是这样,那么您需要在服务器级别添加COR

您可以记录前面提到的成功和失败


您只需将jQuery方法添加到按钮单击事件中,如下所示:

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.getJSON( "example.json", function() {
  console.log( "success" );
})
  .done(function() {
    console.log( "second success" );
  })
  .fail(function() {
    console.log( "error" );
  })
  .always(function() {
    console.log( "complete" );
  });

// Perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function() {
  console.log( "second complete" );
}); 
$(文档).ready(函数(){
$(“#用户输入”).submit(函数(e){
e、 预防默认值();
userQuery=document.getElementById(“查询”).value;
$.getJSON(url+userQuery,函数(数据){
对于(var i=0;i
console.log(data)
的输出是什么?你能添加相关的HTML吗?@ZakariaAcharki nothing它不会输出任何东西我也尝试了一个警报(),但它没有做任何事情。你没有做任何事来阻止
提交。你可以将
e.preventDefault()
submit()
之后,或者将HTML更改为不在
输入上使用
type=“submit”
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.getJSON( "example.json", function() {
  console.log( "success" );
})
  .done(function() {
    console.log( "second success" );
  })
  .fail(function() {
    console.log( "error" );
  })
  .always(function() {
    console.log( "complete" );
  });

// Perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function() {
  console.log( "second complete" );
}); 
$(document).ready(function () {
    $("#userInput").submit(function (e) {
        e.preventDefault();
        userQuery = document.getElementById("query").value;
        $.getJSON(url + userQuery, function (data) {
            for (var i = 0; i < data[1].length; i++) {
                html = '<article><a href="' + data[3][i] + '" target="_blank"><h3>' + data[1][i] + '</h3><p>' + data[2][i] + '</p></a></article>';
                $(".results").append(html);
            }
        });
    });
});