Skip to content
This repository

Fix d3.xhr for local URIs. #632

Merged
merged 2 commits into from about 2 years ago

3 participants

Jason Davies George Adams Mike Bostock
Jason Davies
Collaborator

It turns out there's a simple way to support almost all local file:// requests (where req.status is always 0 in current browsers). The only case where this doesn't work is when the file does actually exist but is empty.

I've tested the CORS use case in Chrome and Safari, and local xhr now works in Safari (it won't work in Chrome at the moment unless you relax the strict security settings).

jasondavies added some commits
Jason Davies jasondavies Fix d3.xhr for local URIs.
The test for local URI is based on jQuery's.  Although Chrome has strict
permissions, other browsers allow local URIs to be retrieved as long as
the same-domain policy is respected e.g. Safari.

We also check for a cross-domain request as req.status === 0 means
failure in this case.
95b1eaa
Jason Davies jasondavies Simpler workaround for local d3.xhr.
If the error flag is set, the response attribute [1] should be the empty
string or null.  So even if a request fails and the response has been
partially loaded, this logic should still work.

This check will fail for local requests where the response entity body
is the empty string, but I don't think it's possible to detect this
situation in current browser implementations. jQuery has the same issue,
for example.

[1]: http://www.w3.org/TR/XMLHttpRequest/#the-response-attribute
53d117e
George Adams
geowa4 commented

+1 on the concept, and the code looks like it'll do the job.

Mike Bostock mbostock merged commit 53d117e into from
Mike Bostock mbostock closed this
Jason Davies
Collaborator

Hurrah! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

May 04, 2012
Jason Davies jasondavies Fix d3.xhr for local URIs.
The test for local URI is based on jQuery's.  Although Chrome has strict
permissions, other browsers allow local URIs to be retrieved as long as
the same-domain policy is respected e.g. Safari.

We also check for a cross-domain request as req.status === 0 means
failure in this case.
95b1eaa
Jason Davies jasondavies Simpler workaround for local d3.xhr.
If the error flag is set, the response attribute [1] should be the empty
string or null.  So even if a request fails and the response has been
partially loaded, this logic should still work.

This check will fail for local requests where the response entity body
is the empty string, but I don't think it's possible to detect this
situation in current browser implementations. jQuery has the same issue,
for example.

[1]: http://www.w3.org/TR/XMLHttpRequest/#the-response-attribute
53d117e
This page is out of date. Refresh to see the latest.

Showing 3 changed files with 5 additions and 5 deletions. Show diff stats Hide diff stats

  1. +1 1  d3.v2.js
  2. +3 3 d3.v2.min.js
  3. +1 1  src/core/xhr.js
2  d3.v2.js
@@ -494,7 +494,7 @@ d3.xhr = function(url, mime, callback) {
494 494 req.onreadystatechange = function() {
495 495 if (req.readyState === 4) {
496 496 var s = req.status;
497   - callback(s >= 200 && s < 300 || s === 304 ? req : null);
  497 + callback(!s && req.response || s >= 200 && s < 300 || s === 304 ? req : null);
498 498 }
499 499 };
500 500 req.send(null);
6 d3.v2.min.js
3 additions, 3 deletions not shown
2  src/core/xhr.js
@@ -7,7 +7,7 @@ d3.xhr = function(url, mime, callback) {
7 7 req.onreadystatechange = function() {
8 8 if (req.readyState === 4) {
9 9 var s = req.status;
10   - callback(s >= 200 && s < 300 || s === 304 ? req : null);
  10 + callback(!s && req.response || s >= 200 && s < 300 || s === 304 ? req : null);
11 11 }
12 12 };
13 13 req.send(null);

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.