0

Given a JSON file at http://codio.io/hugolpz/Cat-is-smart/data/dict.json.

Given I want to load it, then to display it (get my hand on it and use it).

I thus use:

$.getJSON( "http://codio.io/hugolpz/Cat-is-smart/data/dict.json", function( json ) {
  console.log("This step is fired."); // OR NOT !
  alert( "JSON Data: " + JSON.stringify(json) );
 });

or

$.ajax({
        url: "http://codio.io/hugolpz/Cat-is-smart/data/dict.json",
        dataType: 'json',
        success: function(json2) {
            alert( "JSON Data: " + JSON.stringify(json2) );
        }
});

None work, even when tested on same domain.

What do I wrong ? How to fix it.

http://jsfiddle.net/676V5/1/

Hugolpz
  • 17,296
  • 26
  • 100
  • 187
  • 1
    Your JSON file is incorrect. Please remove comment lines from your json file.(Comment lines are not identified by json [link](http://stackoverflow.com/questions/244777/can-i-comment-a-json-file)) – Sai Feb 20 '14 at 15:14

4 Answers4

1

If you use $(document).load like below, you can get json data:

$(document).load( "http://codio.io/hugolpz/Cat-is-smart/data/dict.json", function( json ) 
{
     alert("finish"); // OR NOT !
     alert( "JSON Data: " + JSON.stringify(json) );
});
pegatron
  • 516
  • 4
  • 16
1

Invalid JSON. I suggest using a tool like JSONLint in future, it will save you a lot of hassle!

Ben
  • 7,548
  • 31
  • 45
1

the url you are calling might not returning the json data. It may be in string format. Try by changing "dataType" to "text" instead of "json"

Snehal S
  • 865
  • 4
  • 13
1

Always check for errors...at first it gives me a parseError fail; now it works:

head:

<script type='text/javascript'>
$(document).ready(function(){
    var request = $.ajax({
      url: "http://codio.io/hugolpz/Cat-is-smart/data/dict.json",
      type: "GET",
      dataType: "html"
    });

    request.done(function( msg ) {
      $( "#log" ).html( msg );
    });

    request.fail(function( jqXHR, textStatus ) {
      alert( "Request failed: " + textStatus );
    });
});
</script>

Body:

<div id="log"></div>
Hackerman
  • 12,139
  • 2
  • 34
  • 45