xhr response content-type header - server session timeout

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

xhr response content-type header - server session timeout

zcurtis
In looking at the docs and examining code at runtime, I don't see a way to get the content-type header from a xhr response. I am using 1.10.2 and dojo/request. My objective is to examine the content-type of the response to determine if the server session had previously timed out prior to the user making a ajax call. When the session timeout happens, the content-type header is text/html whereas normally it's either application/json or text/plain. Please advise.
Reply | Threaded
Open this post in threaded view
|

Re: xhr response content-type header - server session timeout

Kenneth G. Franqueiro
I would hope that a request after timeout would also result in an HTTP
error response code, in which case the dojo/request promise will reject
rather than resolve.  Moreover, if you're passing handleAs: 'json' in
your request, it would fail to parse (and reject) if the content
returned isn't actually JSON.  Handling promise rejection would
generally be the preferred way to go about this.

But barring that, you can get response headers from the `response`
promise hung off of the promise returned by dojo/request.  It's
explained under
http://dojotoolkit.org/reference-guide/1.10/dojo/request.html#usage

--Ken

On 3/3/2016 10:16 AM, zcurtis wrote:

> In looking at the docs and examining code at runtime, I don't see a way to
> get the content-type header from a xhr response. I am using 1.10.2 and
> dojo/request. My objective is to examine the content-type of the response to
> determine if the server session had previously timed out prior to the user
> making a ajax call. When the session timeout happens, the content-type
> header is text/html whereas normally it's either application/json or
> text/plain. Please advise.
>
>
>
> --
> View this message in context: http://dojo-toolkit.33424.n3.nabble.com/xhr-response-content-type-header-server-session-timeout-tp4006745.html
> Sent from the Dojo Toolkit mailing list archive at Nabble.com.
>
--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: xhr response content-type header - server session timeout

zcurtis
I read the doc you referenced as well, but I am still can't find the header value...

var def = request.post(aURL, {
        data: domForm.toObject('aId'),
        timeout: 180000
});
def.then(
        function(resp) {
                var ct = def.response.getHeader('Content-type');

In browser:
TypeError: def.response.getHeader is not a function
Reply | Threaded
Open this post in threaded view
|

Re: xhr response content-type header - server session timeout

sg3235
The way I read the docs, def.response is another promise.  So the code would be:
def.response.then(function (data) { console.log(data.getHeader(‘Content-Type’)); });

P.S.  I tried it and it worked, though I won’t guarantee my example has all the parens and curlies matched up.  But the concept is right.

> On Mar 3, 2016, at 4:03 PM, zcurtis <[hidden email]> wrote:
>
> I read the doc you referenced as well, but I am still can't find the header
> value...
>
> var def = request.post(aURL, {
> data: domForm.toObject('aId'),
> timeout: 180000
> });
> def.then(
> function(resp) {
> var ct = def.response.getHeader('Content-type');
>
> In browser:
> TypeError: def.response.getHeader is not a function
> <http://dojo-toolkit.33424.n3.nabble.com/file/n4006752/resp.jpg>
>
>
>
> --
> View this message in context: http://dojo-toolkit.33424.n3.nabble.com/xhr-response-content-type-header-server-session-timeout-tp4006745p4006752.html
> Sent from the Dojo Toolkit mailing list archive at Nabble.com.
> --
> Dojo Toolkit: http://dojotoolkit.org/
> Tutorials: http://dojotoolkit.org/documentation/
>
> [hidden email]
> To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: xhr response content-type header - server session timeout

zcurtis
OK, this does work as you outlined.

From the ref doc, "The provider returns a promise (see dojo/promise/Promise) that is fulfilled with the handled data of the response...The promise returned by the provider has an extra property not on standard promises: response"

I believe what was throwing me off is this extra property dot appear to be in the API doc and that def.response.then(... is used instead of what I was using def.then(...

I don't think I grasp that difference yet and will have to think about it.
Reply | Threaded
Open this post in threaded view
|

Re: xhr response content-type header - server session timeout

dylanks
response is a superset of the normal promise that is returned, and
includes additional details about a request. Both of these promises can
then have then handlers added to them.

on 3/7/16, 17:05 (GMT-07:00) zcurtis said the following:

> OK, this does work as you outlined.
>
> From the ref doc, "The provider returns a promise (see dojo/promise/Promise)
> that is fulfilled with the handled data of the response...The promise
> returned by the provider has an extra property not on standard promises:
> response"
>
> I believe what was throwing me off is this extra property dot appear to be
> in the API doc and that def.response.then(... is used instead of what I was
> using def.then(...
>
> I don't think I grasp that difference yet and will have to think about it.
>
>
>
> --
> View this message in context: http://dojo-toolkit.33424.n3.nabble.com/xhr-response-content-type-header-server-session-timeout-tp4006745p4006759.html
> Sent from the Dojo Toolkit mailing list archive at Nabble.com.
--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Co-Founder, Dojo Toolkit
CEO, SitePen, Inc.  http://www.sitepen.com/