Search code examples
javascriptjquerygetjson

getJSON in Javascript


I am new to html and javascript.As far as i know the following code should give an alert when i press "Get JSON Data" button.But the page is not giving me any response.Any help is greatly appreciated.

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $.getJSON("http://127.0.0.1:5000/2", function(result){
                    if (result.length == 0){
                        alert("nothing") ;
                    }
                    if (result.length){
                        alert("success") ;
                    }    
                    // $("div").append(myObject);
                });
            });
        });
    </script>
</head>
<body>
    <button>Get JSON data</button>
    <div></div>
</body>
</html>


Solution

  • Replace the JSON call with

    $.getJSON("http://127.0.0.1:5000/2", function(result){
            if (result.length == 0){
                alert("nothing") ;
            }
            if (result.length){
                alert("success") ;
            }    
            // $("div").append(myObject);
        }).fail(function( jqxhr, textStatus, error ) {
           var err = textStatus + ", " + error;
           console.log( "Request Failed: " + err )
        });
    

    That way you can see what goes wrong. The javascript looks OK, I suspect it's a server issue.

    You could also try getting back JSON from some random source, like http://1882.no/API/business/get/results.php?q=skole&page=0