SHARE
TWEET

login.js

a guest Nov 6th, 2013 213 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).on('click','#page_login_submit',function(){
  2.  
  3.   var name = $('#page_login_name').val();
  4.   if (!name) { alert('Please enter your user name.'); return false; }
  5.   var pass = $('#page_login_pass').val();
  6.   if (!pass) { alert('Please enter your password.'); return false; }
  7.  
  8.   // BEGIN: drupal services user login (warning: don't use https if you don't have ssl setup)
  9.  
  10.    $.ajax({
  11.               url:"http://10.0.2.2:81/drupal/services/session/token",
  12.               type:"get",
  13.               dataType:"text",
  14.                error: function(jqXHR, textStatus, errorThrown) {
  15.                       console.log(JSON.stringify(XMLHttpRequest));
  16.                       console.log(JSON.stringify(textStatus));
  17.                       console.log(JSON.stringify(errorThrown));
  18.                     },
  19.                     success: function (token) {
  20.       $.ajax({
  21.       url: "http://10.0.2.2:81/drupal/test/user/login.json",
  22.        type: 'post',
  23.             data: 'username=' + encodeURIComponent(name) + '&password=' + encodeURIComponent(pass),
  24.             dataType: 'json',
  25.             beforeSend: function (request) {
  26.             alert(token);
  27.              request.setRequestHeader("X-CSRF-Token", token);
  28.                   },
  29.       error: function(jqXHR, textStatus, errorThrown) {
  30.         alert('page_login_submit - failed to login');
  31.         alert(JSON.stringify(jqXHR));
  32.         alert(JSON.stringify(textStatus));
  33.         console.log(JSON.stringify(errorThrown));
  34.       },
  35.       success: function (data) {
  36.        $.mobile.changePage("index.php", "slideup");
  37.       }});
  38.   }
  39.   });
  40.  
  41.  
  42.   // END: drupal services user login
  43.   return false;
  44. });
RAW Paste Data
Want to get better at JavaScript?
Learn to code JavaScript in 2017
Top