1.0.6 • Published 7 years ago

d3-request v1.0.6

Weekly downloads
194,982
License
BSD-3-Clause
Repository
github
Last release
7 years ago

d3-request

This module provides a convenient alternative to XMLHttpRequest. For example, to load a text file:

d3.text("/path/to/file.txt", function(error, text) {
  if (error) throw error;
  console.log(text); // Hello, world!
});

To load and parse a CSV file:

d3.csv("/path/to/file.csv", function(error, data) {
  if (error) throw error;
  console.log(data); // [{"Hello": "world"}, …]
});

To post some query parameters:

d3.request("/path/to/resource")
    .header("X-Requested-With", "XMLHttpRequest")
    .header("Content-Type", "application/x-www-form-urlencoded")
    .post("a=2&b=3", callback);

This module has built-in support for parsing JSON, CSV and TSV; in browsers, but not in Node, HTML and XML are also supported. You can parse additional formats by using request or text directly.

Installing

If you use NPM, npm install d3-request. Otherwise, download the latest release. You can also load directly from d3js.org, either as a standalone library or as part of D3 4.0. AMD, CommonJS, and vanilla environments are supported. In vanilla, a d3 global is exported:

<script src="https://d3js.org/d3-collection.v1.min.js"></script>
<script src="https://d3js.org/d3-dispatch.v1.min.js"></script>
<script src="https://d3js.org/d3-dsv.v1.min.js"></script>
<script src="https://d3js.org/d3-request.v1.min.js"></script>
<script>

d3.csv("/path/to/file.csv", callback);

</script>

Try d3-request in your browser.

API Reference

# d3.request(url, callback) <>

Returns a new request for specified url. If no callback is specified, the returned request is not yet sent and can be further configured. If a callback is specified, it is equivalent to calling request.get immediately after construction:

d3.request(url)
    .get(callback);

If you wish to specify a request header or a mime type, you must not specify a callback to the constructor. Use request.header or request.mimeType followed by request.get instead. See d3.json, d3.csv, d3.tsv, d3.html and d3.xml for content-specific convenience constructors.

# request.header(name, value) <>

If value is specified, sets the request header with the specified name to the specified value and returns this request instance. If value is null, removes the request header with the specified name instead. If value is not specified, returns the current value of the request header with the specified name. Header names are case-insensitive.

Request headers can only be modified before the request is sent. Therefore, you cannot pass a callback to the request constructor if you wish to specify a header; use request.get or similar instead. For example:

d3.request(url)
    .header("Accept-Language", "en-US")
    .header("X-Requested-With", "XMLHttpRequest")
    .get(callback);

Note: this library does not set the X-Requested-With header to XMLHttpRequest by default. Some servers require this header to mitigate unwanted requests, but the presence of the header triggers CORS preflight checks; if necessary, set this header before sending the request.

# request.mimeType(type) <>

If type is specified, sets the request mime type to the specified value and returns this request instance. If type is null, clears the current mime type (if any) instead. If type is not specified, returns the current mime type, which defaults to null. The mime type is used to both set the "Accept" request header and for overrideMimeType, where supported.

The request mime type can only be modified before the request is sent. Therefore, you cannot pass a callback to the request constructor if you wish to override the mime type; use request.get or similar instead. For example:

d3.request(url)
    .mimeType("text/csv")
    .get(callback);

# request.user(value) <>

If value is specified, sets the user name for authentication to the specified string and returns this request instance. If value is not specified, returns the current user name, which defaults to null.

# request.password(value) <>

If value is specified, sets the password for authentication to the specified string and returns this request instance. If value is not specified, returns the current password, which defaults to null.

# request.timeout(timeout) <>

If timeout is specified, sets the timeout attribute of the request to the specified number of milliseconds and returns this request instance. If timeout is not specified, returns the current response timeout, which defaults to 0.

# request.responseType(type) <>

If type is specified, sets the response type attribute of the request and returns this request instance. Typical values are: (the empty string), arraybuffer, blob, document, and text. If type is not specified, returns the current response type, which defaults to .

# request.response(value) <>

Sets the response value function to the specified function and returns this request instance. The response value function is used to map the response XMLHttpRequest object to a useful data value. See the convenience methods json and text for examples.

# request.get(data) <>

Equivalent to request.send with the GET method:

request.send("GET", data, callback);

# request.post(data) <>

Equivalent to request.send with the POST method:

request.send("POST", data, callback);

# request.send(method, data) <>

Issues this request using the specified method (such as GET or POST), optionally posting the specified data in the request body, and returns this request instance. If a callback is specified, the callback will be invoked asynchronously when the request succeeds or fails. The callback is invoked with two arguments: the error, if any, and the response value. The response value is undefined if an error occurs. This is equivalent to:

request
    .on("error", function(error) { callback(error); })
    .on("load", function(xhr) { callback(null, xhr); })
    .send(method, data);

If no callback is specified, then "load" and "error" listeners should be registered via request.on.

# request.abort() <>

Aborts this request, if it is currently in-flight, and returns this request instance. See XMLHttpRequest’s abort.

# request.on(type, listener) <>

If listener is specified, sets the event listener for the specified type and returns this request instance. If an event listener was already registered for the same type, the existing listener is removed before the new listener is added. If listener is null, removes the current event listener for the specified type (if any) instead. If listener is not specified, returns the currently-assigned listener for the specified type, if any.

The type must be one of the following:

  • beforesend - to allow custom headers and the like to be set before the request is sent.
  • progress - to monitor the progress of the request.
  • load - when the request completes successfully.
  • error - when the request completes unsuccessfully; this includes 4xx and 5xx response codes.

To register multiple listeners for the same type, the type may be followed by an optional name, such as load.foo and load.bar. See d3-dispatch for details.

# d3.csv(url[, row, callback]) <>

Returns a new request for the CSV file at the specified url with the default mime type text/csv. If no callback is specified, this is equivalent to:

d3.request(url)
    .mimeType("text/csv")
    .response(function(xhr) { return d3.csvParse(xhr.responseText, row); });

If a callback is specified, a GET request is sent, making it equivalent to:

d3.request(url)
    .mimeType("text/csv")
    .response(function(xhr) { return d3.csvParse(xhr.responseText, row); })
    .get(callback);

An optional row conversion function may be specified to map and filter row objects to a more-specific representation; see dsv.parse for details. For example:

function row(d) {
  return {
    year: new Date(+d.Year, 0, 1), // convert "Year" column to Date
    make: d.Make,
    model: d.Model,
    length: +d.Length // convert "Length" column to number
  };
}

The returned request exposes an additional request.row method as an alternative to passing the row conversion function to d3.csv, allowing you to configure the request before sending it. For example, this:

d3.csv(url, row, callback);

Is equivalent to this:

d3.csv(url)
    .row(row)
    .get(callback);

# d3.html(url, callback) <>

Returns a new request for the HTML file at the specified url with the default mime type text/html. The HTML file is returned as a document fragment. If no callback is specified, this is equivalent to:

d3.request(url)
    .mimeType("text/html")
    .response(function(xhr) { return document.createRange().createContextualFragment(xhr.responseText); });

If a callback is specified, a GET request is sent, making it equivalent to:

d3.request(url)
    .mimeType("text/html")
    .response(function(xhr) { return document.createRange().createContextualFragment(xhr.responseText); })
    .get(callback);

HTML parsing requires a global document and relies on DOM Ranges, which are not supported by JSDOM as of version 8.3; thus, this method is supported in browsers but not in Node.

# d3.json(url, callback) <>

Returns a new request to get the JSON file at the specified url with the default mime type application/json. If no callback is specified, this is equivalent to:

d3.request(url)
    .mimeType("application/json")
    .response(function(xhr) { return JSON.parse(xhr.responseText); });

If a callback is specified, a GET request is sent, making it equivalent to:

d3.request(url)
    .mimeType("application/json")
    .response(function(xhr) { return JSON.parse(xhr.responseText); })
    .get(callback);

# d3.text(url, callback) <>

Returns a new request to get the text file at the specified url with the default mime type text/plain. If no callback is specified, this is equivalent to:

d3.request(url)
    .mimeType("text/plain")
    .response(function(xhr) { return xhr.responseText; });

If a callback is specified, a GET request is sent, making it equivalent to:

d3.request(url)
    .mimeType("text/plain")
    .response(function(xhr) { return xhr.responseText; })
    .get(callback);

# d3.tsv(url[, row, callback]) <>

Returns a new request for a TSV file at the specified url with the default mime type text/tab-separated-values. If no callback is specified, this is equivalent to:

d3.request(url)
    .mimeType("text/tab-separated-values")
    .response(function(xhr) { return d3.tsvParse(xhr.responseText, row); });

If a callback is specified, a GET request is sent, making it equivalent to:

d3.request(url)
    .mimeType("text/tab-separated-values")
    .response(function(xhr) { return d3.tsvParse(xhr.responseText, row); })
    .get(callback);

An optional row conversion function may be specified to map and filter row objects to a more-specific representation; see dsv.parse for details. For example:

function row(d) {
  return {
    year: new Date(+d.Year, 0, 1), // convert "Year" column to Date
    make: d.Make,
    model: d.Model,
    length: +d.Length // convert "Length" column to number
  };
}

The returned request exposes an additional request.row method as an alternative to passing the row conversion function to d3.tsv, allowing you to configure the request before sending it. For example, this:

d3.tsv(url, row, callback);

Is equivalent to this:

d3.tsv(url)
    .row(row)
    .get(callback);

# d3.xml(url, callback) <>

Returns a new request to get the XML file at the specified url with the default mime type application/xml. If no callback is specified, this is equivalent to:

d3.request(url)
    .mimeType("application/xml")
    .response(function(xhr) { return xhr.responseXML; });

If a callback is specified, a GET request is sent, making it equivalent to:

d3.request(url)
    .mimeType("application/xml")
    .response(function(xhr) { return xhr.responseXML; })
    .get(callback);

XML parsing relies on xhr.responseXML which is not supported by node-XMLHttpRequest as of version 1.8; thus, this method is supported in browsers but not in Node.

@dterekhova/taskshiglass-galaxydataframe-js-ly@infinitebrahmanuniverse/nolb-d3zh-escher@everything-registry/sub-chunk-1437ta-react-trackingta-react-article-teasersta-react-polymorphic-copyright-noticeta-react-polymorphic-mastheadta-react-polymorphic-share-buttonsta-react-polymorphic-trackingxform-uploadersagan.glvr-visualizationvr-vizloaders.glid-mapeojxy-dfkepler.gl-forked-ismakutlkepler.gl.geoiqkepler.gl.sh.customktapihiglass-vcfhiglass-bigwig-datafetcherhiglass-cohorthiglass-general-vcfhiglass-gnomadhiglass-ithiglass-no-github-depshiglass-pileuphiglass-pluginshiglass-svhiglassideogram-tthekla-viewerleaflet.tilelayer.gloperationsglobe-vismapasmb-isochronenode-user-accounts-client-boilerplatep3.jsmymap.glradviz-componentramesh-paul-kepler.glske-vizsmaht-higlass-miscsolarnetwork-test-utilstruekepler.gl@ta-interaktiv/react-polymorphic-tracking@carto/kepler.gl@!!!!!/polarbear@adachi-koichi/googlemapvue@dd-decaf/escher@datenguide/explorables@ethubert/kepler.gl@eqworks/kepler.gl@khang00/kepler.gl@kepler.gl/tasks@schmoo/comms@redsift/d3-rs-geo@nteract/vega-embed-v3@sugarcrm/d3-sugar@ta-interaktiv/react-pollingresults@ta-interaktiv/react-polymorphic-article-teasers@reservoirgenomics/resglass@sebf/kepler.gl@shitao1988/swsk-kepler-gl@shitao1988/swsk.kepler.gl@tfm-adrianarenal/forced-bubbles-viz@tfm-adrianarenal/pie-chart@tfm-adrianarenal/progress-bar-chart@tfm-adrianarenal/bar-chart@thisisbarney/request@uhaider15/tasks@tommy2gis/swsk.kepler.glcircle-packing-timelinedataframe-jsdataframe-js-sortember-cli-d3-primitivebraheboide-kepler-glbaba-jscalculadora-de-creditosd3plus-vizd3nicd3v4d3v410d3.promised3-jetpackescherescher-visescher-vmherasme-kepler.glgalvisgeospatial-data-viz-backendgeospatial-data-viz-webapp
1.0.6

7 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.5.0

8 years ago

0.4.7

9 years ago

0.4.6

9 years ago

0.4.5

9 years ago

0.4.4

9 years ago

0.4.3

9 years ago

0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.6

9 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago