Skip to content

Instantly share code, notes, and snippets.

@omarkj
Created March 16, 2011 15:01
Show Gist options
  • Save omarkj/872618 to your computer and use it in GitHub Desktop.
Save omarkj/872618 to your computer and use it in GitHub Desktop.
XHR with credentials across subdomains

First, your HTTP server must return the correct HTTP Access Control rules

  Access-Control-Allow-Origin: "http://subdomain.domain.tld"
  Access-Control-Allow-Methods: "GET"
  Access-Control-Allow-Credentials: "true"
  Access-Control-Allow-Headers: "X-Requested-With"

During your AJAX request, set withCredentials to true

  $.ajax
    url: kodi.config.user_db
    xhrFields: {'withCredentials': true}
    success: (ctx) =>
      return true if ctx.username is not null
@crcastle
Copy link

crcastle commented Jul 4, 2011

ok, thanks. still struggling with this. i'll post back here if i get it working.

@omarkj
Copy link
Author

omarkj commented Jul 11, 2011

Did it work out for you?

@crcastle
Copy link

No, unfortunately. instead i send the user's browser to the underlying server i'm trying to get data from. this makes the browser pop an http basic auth dialog. the user fills this in and is then redirected back to the site from which he came. this works because the browser remembers basic auth credentials until the browser is closed. so for example, user lands at http://xyz.com. the html/js/css served from xyz.com is trying to make get/post/update/delete requests to http://abc.com. on the first attempt, the user is sent to http://abc.com/login. after successful login, user is sent back to http://xyz.com and has to redo his previous request.

don't really like it, but it works...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment