1

how do I retrieve the response headers in Dojo 1.6? dojo.xhr returns a dojo.Deferred object and when I register a closure with then() the first argument contains only the response body. I know that the headers are in the ioArgs property but I cannot reach it from inside the closure. Am I approaching this in a wrong way?

Best regards, CQQL

Marten
  • 1,336
  • 10
  • 16

2 Answers2

4

So here is how I solved it using a closure:

var result = dojo.xhr(
    "GET",
    {
         url: "http://example.com"
    }
);

result.then(function (response) {
    console.log(result.ioArgs.xhr.getAllResponseHeaders());
});

But for a clean solution the ioArgs should definitely be passed as part of the response object.

Marten
  • 1,336
  • 10
  • 16
1

Maybe you can do this:

var deferred = dojo.xhrGet({
    url: 'myurl',
    handle: function(res, io) { globalIOVar = io; }
});

deferred.then(
    function(res) {
        // Can access ioargs via globalIOVar...
    }
);

I don't think ioArgs are passed as second argument of closure passed to deferred.then, it would be easier, and would make more sense.

faken
  • 6,572
  • 4
  • 27
  • 28
  • The problem is that handle is not documented in dojo1.6. So it seems to be deprecated. – Marten Jun 13 '11 at 22:32
  • I am not able to answer my own question right now (reputation ...), but I solved it and will post the answer tomorrow :) – Marten Jun 13 '11 at 22:36
  • handle is not deprecated, you can check in this tutorial for Dojo 1.6: http://dojotoolkit.org/documentation/tutorials/1.6/ajax/ – faken Jun 13 '11 at 22:48
  • but it is not documented in the api documentation: http://dojotoolkit.org/api/1.6/dojo/xhr – Marten Jun 14 '11 at 00:00
  • This solution would work, but you'd want to be careful with regards to handling the global IO variable. It'd likely be better (if possible) to contain the return of the `xhrGet` and the `.then` callback within a closure, and doing something like `var myIOargs` within that space, to keep from exposing it into global space. – Brian Arnold Sinclair Jun 14 '11 at 00:24
  • load and error are also not referred in the API, strange. Nonetheless, load, error and handle are essential for simple xhr. This solution is of course a quick hack, which can surely be improved. @CQQL, when you can, post your solution. Best regards. – faken Jun 14 '11 at 01:10