在 jquery 中加载 Json 文件在以下代码中失败

Loading Json file in jquery fails in the following code

我遇到了一个大问题!

我正在尝试使用 Jquery 加载 Json 文件,但总是失败! 我尝试过很多不同的东西,但对我没有任何用处。我也不确定如何真正调试它,出了什么问题!

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready( function() {
    var url = "content.json";
    var outp = {
                    low         :   0,
                    high        :   99,
                    name        :   "Fritz",
                    rufnummer   :   "012",
                    faxnummer   :   "345",
                    mobil       :   "678",
                    mail        :   "mail@mail.mail",  
                };

    $('#find').on("click", function(){

        var data = $.getJSON( url, function() {
          console.log( "success" );
        })
          .done(function() {
            console.log( "second success" );
          })
          .fail(function() {
            console.log( "error" );
          })
          .always(function() {
            console.log( "complete" );
          });

            console.log(data);      
            console.log(outp);
            console.log("Hi");

           data.complete(function() {
             console.log( "second complete" );
           });
        });
    });
//});
</script>
</head>
<body>
    <p>Postleitszahl:</p>
    <input type="number" autocomplete="on" name="inp" id="inp">
    <button type="button" id="find">Finden</button>
    <p class="output"></p>
</body>
</html>

还有我的 JSON:

{
    "low"         :   0,
    "high"        :   99,
    "name"        :   "Fritz",
    "rufnummer"   :   "012",
    "faxnummer"   :   "345",
    "mobil"       :   "678",
    "mail"        :   "mail@mail.mail",  
}

而不是:

var data = $.getJSON( url, function() {...})

试试:

$.getJSON( url, function(data) {...})

你可以像 Roxoradev 的答案那样做,或者试试这个:

data.complete(function() {
   console.log( data );
});

而不是

data.complete(function() {
   console.log( "second complete" );
});

解释

你的延迟现在存储在 data 如果你想解决它用 .complete(func).done(func)

调用它

来自文档:

The Promise interface in jQuery 1.5 also allows jQuery's Ajax methods, including $.getJSON(), to chain multiple .done(), .always(), and .fail() callbacks on a single request, and even to assign these callbacks after the request may have completed. If the request is already complete, the callback is fired immediately.

所以你也可以写成.done()