mbostock/d3 — pull request #814 by ZJONSSON (2012-09-19 13:36:02+00:00)
Title: Xhr2

The new XHR2 framework looks great. Here are a few additional ideas:

### xhr.post convenience function
Sending a POST request involves the following steps:
* Specify "POST" as a method in the open()
* Set the Content-Encoding header correctly
* Ensure that the supplied data is a string.

Function xhr.post() simplifies the process by re-opening the request, determining encoding / set header and stringify data if necessary. Any parsing convenience function (d3.json/d3.text /d3.xml etc) can be switched to a POST by appending .post(data) to the function call.

For example, the following GET
```
d3.csv("http://localhost/test?a=1&b=2",fn)
```
can be changed to POST (url-encoded)
```
d3.csv("http://localhost/testprocess",fn)
.post("a=1&b=2")
```
or POST (json-encoded)
```
d3.csv("http://localhost/test",fn)
.post({a:1,b:2})
```

### Accept Header moved to xhr.open
If mime has been defined at the top level, we use when xhr.open is called (in case it's called twice) as is the case when using the parsing convenience functions (d3.csv/d3.xml/d3.text etc) with a redefined xhr.open (i.e. xhr.post)

### sendTimeout
By putting a setTimeout(...,0) on the xhr.send and canceling/reinstating upon changes we eliminate the occurance of multiple firings of send , allowing the send function to be anywhere in the chain, not just at the end. In node this would be done under process.nextTick(). I realize this is unconventional, but it seems a very effective solution given how javascript sequences execution.

### xhr.open username/password
The XMLHttpRequest allows the specification of username /password. This option is simply added to xhr.open and passed on.