简体   繁体   中英

jQuery.getJSON not returning anything.

I'm trying to call a json file, but my function isnt returning anything.

index.html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script>$(document).ready(function(){
    $.getJSON( 'ebooks.json', function( fb ) {
        alert(fb);
    });          
});
}

ebooks.json

{
"title" : "software design"
}

Can you try this, You have added extra } in your code,

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>

 <script>

    $(document).ready(function(){
        $.getJSON( 'ebooks.json', function( fb ) {
            alert(fb);
        });          
    });

</script>

You can able to find this errors using in Firefox Tools->Web Developer ->Error Console or CTRL+SHIFT+J

Don't know why to be honest, but it only worked when I declared the ready() function separately and passed this function to $(document).ready.

<html>
<body>
    <h1 id="titel">Title</h1>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script type="text/javascript">
    function ready() {
        $.getJSON( 'ebooks.json', function( fb ) {
            alert(fb.title);
        });          
    };

    $(document).ready(ready());

</script>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM