2

I'm using d3.json() to request a json object and parse/visualize it...

It's being requested from a java servlet doGet function...I know for a fact that my java server is successful sending the json to the browser. However, I'm running into a problem trying to parse the json. d3.json is supposed to do an http get request, but for some reason, it is returning an XML request error:

XMLHttpRequest cannot load "http://localhost:8080/cluster". Origin null is not allowed by Access-Control-Allow-Origin.

Is there some further set up I should know about with d3.json or is it supposed to just work out of the box? Here is my code..

    d3.json("http://localhost:8080/cluster", function(json) {       

            //do visualization
            var node = vis.data([json]).selectAll("g.node")
             .data(pack.nodes)
             .enter().append("g")
             .attr("class", function(d) {return d.children ? "node" : "leaf node" ;})
             .attr("transform", function(d) {return "translate(" + d.x + "," + d.y +")"; });

            node.append("title")
                .text(function(d) { return d.name + (d.children ? "" : ": " + format(d.size)); });



            node.append("circle")
            .attr("r", function(d) { return d.r; });

            node.filter(function(d) { return !d.children; }).append("text")
            .attr("text-anchor", "middle")
            .attr("dy", ".3em")
            .text(function(d) { return d.name.substring(0, d.r / 3); });
    }

Am I doing something wrong here? I'm mainly confused because d3.json is supposed to do an HTTP get by default, but its returning an XMLHTTPrequest error...

3
  • 1
    It's not related to D3, any XMLHTTPRequest you make to another domain will throw this error if access is not allowed. How are you loading the page containing this code? Jul 24, 2012 at 0:03
  • I see...how do I go about allowing cross-orgin resource sharing? Is this something that should be set in the javascript or my jetty server?
    – capkutay
    Jul 24, 2012 at 0:08
  • 1
    Your server has to set the appropriate HTTP header. See en.wikipedia.org/wiki/Cross-origin_resource_sharing. But if the server is also serving this page then you should have to do this. Jul 24, 2012 at 0:11

1 Answer 1

0

If you are loading you HTML file using the file protocol (file:///) then you will run into this issue. Try to load the html file also using localhost, and that should resolve the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.