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/

有帮助吗?

解决方案

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

其他提示

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) );
});

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"

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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top