1

I have an html5 app which logs the user into drupal via services 3.11 but I am unable to connect.

Using the Chrome Postman plugin I can connect as expected. The difference between Postman and my code is that the cookie session is not being passed from my app. From what I have read the withCredential: true is supposed to pass the session cookie but it's not working for me?

  1. api/user/login.json - Logs the user in as expected
  2. api/user/token.json - returns the token as expected
  3. api/system/connect.json - Always returns the data for Anonymous user 0

The system/connect code sent after login and token is:

                jQuery.ajax({
                    type: "POST",
                    url: connectUrl,
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (result) {
                        dosomething();
                    },
                    error: function (result) {
                        dosomething();
                    },
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('X-CSRF-Token', Session.user_token);
                    },
                });
  • I'm voting to close this question as off-topic because it is about code you wrote, for which you want to write better code, or you want to understand why it doesn't work, but you didn't show the code you are using, or the part of the code that is relevant for the question. – Mołot Mar 1 '15 at 18:28
  • Not sure I follow - The code shown is the code that is failing and yes, I would like to understand why it doesn't work. Just to clarify, I've been using the same snippet (taken from the jQuery examples) to do ajax posts with no problem. It was only after trying it with cross-origin that it started to fail. After researching extensively I'd learned that 'withCredentials: true' is supposed to pass the session cookie, but for some reason it's not doing it. – nakeddesign Mar 2 '15 at 20:57
1

For anyone else who stumbles across this problem I did finally manage to solve it with the following which ensures the credentials are pass for each call consistently. I'm assuming where CORS adds in the additional OPTIONS request, the credentials we're going through.

jQuery.support.cors = true;
    jQuery.ajaxSetup({
        xhrFields: {
            withCredentials: true   // Pass session cookie with requests
        }
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.