5

Hi so i'm in the middle of learning the D3 Framework, but no matter what I do, I can't seem to load my Json file. I'm running the script on a server and the JSON file is in the same directory as the html/d3 file im running it from but it keeps giving me a 404 error when I look at the console.

So Im running it on www.mywebsite.com, and then I have the Json file stored in www.mywebsite.com/mydata.json
Can anyone help me out?

<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> 
</head>
<body>
    <script type="text/javascript">

    d3.json("mydata.json", function(data){ 

        var canvas= d3.select("body").append("svg")
        .attr("width",500)
        .attr("height", 500)

        canvas.selectAll("rect")
            .data(data)
            .enter()
                .append("rect")
                .attr("width", function(d){ return d.age *10;})
                .attr("height", 50)
                .attr("y", function(d ,i){ return i* 50; })
                .attr("fill", "blue");
    });

    </script>
mross1080
  • 144
  • 1
  • 7
  • Are this html file and mydata.json in the same directory? – bnuhero Jan 09 '14 at 00:28
  • Yeah both locally and on the server, im pretty sure the problem is that the JSON file that I wrote is being saved as a text/html file. I saved it as mydata.json but because I wrote it in sublime 2 i think there was a problem – mross1080 Jan 09 '14 at 02:09

2 Answers2

4

Take a look at this and this gotchas. Let us know if you succeeded accessing json after that.

Community
  • 1
  • 1
VividD
  • 10,456
  • 6
  • 64
  • 111
  • So I followed the advice of one of the guides, I poked around the debugger and it turns out my JSON file is being saved as Text/html even though im giving it the mydata.json file type. I wrote it in sublime 2, do you know any fixes to this? – mross1080 Jan 09 '14 at 02:07
  • The fix is either your file will be of appropriate json type or you just rename *.json to *.txt. I wouldn't bother finding root cause why Sublime Text 2 or any other editor saved your json as text/html type, and I think neither should you... :) The most important thing is: can you display data on your web page? – VividD Jan 09 '14 at 02:14
  • So instead of d3.json("mydata.json", function(data){ I should save as txt and put d3.json("mydata.txt", function(data)? I doubt my text editor is the problem im just confused on how to get it in the right format. Thanks for the help – mross1080 Jan 09 '14 at 02:20
  • I have no idea why that was the solution but it worked! Thanks so much, if you have a moment do you know why saving it as a text file allowed the JSON to be found? – mross1080 Jan 09 '14 at 02:36
1

You guys need not change any Formats. Just need to change some settings in Server.Here I am using IIS server so I have tested my files and got same 404 error for JSON Files and I made the following changes and it worked like a Charm. You just follow this If you are using an IIS Server...

Step 1: Open IIS Server and go to MIME Types

enter image description here

Step2: Click Add from right side and add JSON extension and restart the server

enter image description here

Gvs Akhil
  • 2,165
  • 2
  • 16
  • 33