rest - Drupal 7 Services 3.4 with session authentication and jQuery -


i'm having trouble connecting drupal 7 , jquery using services 3.4 , jquery cookie plugin. understand, need following: - post service endpoint /user/login - session name , session id , add them http cookie - session token id - add token id http header: x-csrf-token: sometoken

i try method using jquery , receive 'access denied user anonymous' error. i'm using services 3.4 cors module across 2 subdomains. endpoint appears set correctly , login function returns user , session data, , token.

i have tested accessing service php script based on this example. modified example create nodes well. works expected, respecting drupal's permissions.

i have made following change header in custom module after receiving errors token being in header.

function custom_services_init() {     drupal_add_http_header('access-control-allow-headers', 'x-csrf-token'); } 

here jquery code:

$('#menu-connect').click(function() {     var url = 'http://myservice.com/service_endpoint/user/login.json';     $.post(url, { username: 'testuser', password: 'password' }, function(data) {         sessname = data.session_name;         sessid = data.sessid;          $.cookie(sessname, sessid);          // obtain session token.         $.ajax({           url:"http://myservice.com/services/session/token",           type:"get",           datatype:"text",           error:function (jqxhr, textstatus, errorthrown) {             alert(errorthrown);           },           success: function (token) {                              $.ajax({               url: 'http://myservice.com/service_endpoint/user/1.json',               type: "get",               datatype: "json",               beforesend: function (request) {                 request.setrequestheader("x-csrf-token", token);               },               error: function (jqxhr, textstatus, errorthrown) {                 alert(errorthrown);               },               success: function (data) {                 alert('hello user #' + data.user.uid);               }             });           }         });          });       }); 

just addition included in comment #60 @ https://drupal.org/node/2013781#comment-7764881 (but missed 10-times or so).

in jquery, have set credentials passed. otherwise, never cookie server remember you.

    $.ajaxsetup({         xhrfields: {             withcredentials: true         }     }); 

**note: usage affect all ajax calls (which, in cases, want, use above method). if need different, include in $.ajax parameters.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -