문제

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