Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Requests with credentials (add withCredentials to xhr) #40

Closed
webpro opened this issue Sep 7, 2013 · 8 comments
Closed

Requests with credentials (add withCredentials to xhr) #40

webpro opened this issue Sep 7, 2013 · 8 comments

Comments

@webpro
Copy link

webpro commented Sep 7, 2013

How should I do an XHR/XDR that has withCredentials set to true? Basically I need it to send cookies along with the request (cookies were set server-side).

In plain JS I can do:

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

Here's what I tried with rest.js:

var myFirstInterceptor = interceptor({
    request: function handleRequest(request, config) {
        request.withCredentials = true;
        return request;
    }
});

var client = rest.chain(myFirstInterceptor);
client({path: '/foo'});

However, the request object here is not the "raw" XMLHTTPRequest, nor is the property copied onto the actual XHR object.

Can you help me out here? :-)

@scothis
Copy link
Member

scothis commented Sep 7, 2013

Right now there is no way to do what you're trying to do with rest.js. Although, it's easy enough to copy the withCredentials on to the actual xhr inside https://github.com/cujojs/rest/blob/master/client/xhr.js.

It's worth noting that only XHR2 compatible browsers know what to do with the property. rest.js doesn't somehow make an older browser handle CORS.

I'll add this before the next release

@webpro
Copy link
Author

webpro commented Sep 7, 2013

It's worth noting that only XHR2 compatible browsers know what to do with the property. rest.js doesn't somehow make an older browser handle CORS.

Sure, but still it should be possible, imho.

I'll add this before the next release

Thanks! Appreciated.

@scothis
Copy link
Member

scothis commented Sep 16, 2013

Trying to think through the best way to approach this. I think there are basically two options:

  1. create a 'withCredentials' property on the rest.js request that is set on the XHR
  2. create a 'mixins' object on the rest.js request that can contain any values that will be copied into the XHR

Option 1 is clearly specific to this need and only relevant to the XHR client. Option 2 has a chance of being applicable in other environments, and supporting other properties.

Thoughts @webpro ?

@webpro
Copy link
Author

webpro commented Sep 16, 2013

Definitely the 2nd option (there's plenty of attributes that can be set on the XHR(2) object). For what it's worth, that's also how jQuery does it.

scothis added a commit that referenced this issue Sep 16, 2013
There are flags on the XHR that are important to set correctly in certain
contexts. The 'withCredentials' property used by CORS is one example. As
the context is essential, there is no good default value, thus we need to
make it open to configuration. Rather than explicitly support each property,
the xhr client now supports `request.mixin` which will copy all its
properties directly on to the raw XHR object.

Issue: #40
scothis added a commit that referenced this issue Sep 17, 2013
There are flags on the XHR that are important to set correctly in certain
contexts. The 'withCredentials' property used by CORS is one example. As
the context is essential, there is no good default value, thus we need to
make it open to configuration. Rather than explicitly support each property,
the xhr client now supports `request.mixin` which will copy all its
properties directly on to the raw XHR object.

Issue: #40
@scothis
Copy link
Member

scothis commented Sep 18, 2013

@webpro I have a fix on the dev branch if you wouldn't mind giving it a spin. One curious bit is that adding any new property to the XHR object in IE 6 causes the assignment to throw. So I added a check that the property exists on the XHR object before doing the assignment.

@unscriptable
Copy link
Member

haha, yah. in IE6, the xhr object is a "native" object, not a JavaScript object.

@scothis
Copy link
Member

scothis commented Sep 19, 2013

Fix is on the dev branch and will be part of the next release. Please reopen if this doesn't fit your needs.

@scothis scothis closed this as completed Sep 19, 2013
@webpro
Copy link
Author

webpro commented Sep 19, 2013

Thanks a bunch, @scothis! Just tested with the dev branch and works great for me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants