문제

I need to tell Ajax.Request (from Prototype.js) to set xhr.transport.withCredentials to true (to enable cookies in my cross site requests headers). I failed by trying:

Ajax.Request('http://otherSubdomain.host:port/', {
    onCreate: function(request){
        request.transport.withCredentials = true;
    }
});

Access-Control-Allow-Origin is set and the request is successful, but no cookies were sent.

I hate to point out, but it seems to be much easier with jquery here is an example solution.

도움이 되었습니까?

해결책

Try to patch Ajax.Request like this:

Ajax.Request.prototype.request = Ajax.Request.prototype.request.wrap(function(request, url) {
  if (this.options.withCredentials) {
    this.transport.withCredentials = true;
  }
  request(url);
});

And then you'll have additional option withCredentials:

new Ajax.Request('example.com', {
    withCredentials: true
});

다른 팁

Improved Victor's answer a bit.

Fixes in IE where it requires setting withCredentials between 'open' and 'send' and makes it consistent with jQuery ajax options.

Ajax.Request.prototype.setRequestHeaders = Ajax.Request.prototype.setRequestHeaders.wrap(function(setHeaders) {
  setHeaders();
  if (this.options.xhrFields) Object.extend(this.transport, this.options.xhrFields);
});

And then to use it:

new Ajax.Request('example.com', {
  xhrFields: {
    withCredentials: true
  }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top