2

Hi,

I'm very new to Drupal JSON web service and JQuery.

I have a JSON Web Service (http://www.myweb.com/services/json) and which has method called user.login.

The method user.login takes 2 params, username and password.

I tried to access the method using CURL commend

curl --data method="user.login" --data username="uname" password="DE"
       http://www.myweb.com/services/json

It Works Fine.

Now, I want to access this method using jQuery.

I tried using jQuery Ajax Methods,

$.post("http://www.myweb.com/services/json",
        'method=user.login&username=uname&password=de',
         function(data){
        alert(data);
    });

and

$.ajax({
    url: "http://www.myweb.com/services/json",
    type: 'POST',
    data:'method=user.login&username=uname&password=de',
    contentType: 'application/json',
    success: function(data,textStatus, jqXHR){
        alert(data + " " + textStatus + " " +jqXHR);
    }});

But success callback method not getting call. What is the problem with this code ? and How can I call user.login method using jQuery?

Can someone help me ?, plz

Thanks

2 Answers 2

2

See this, here they have shown how to use login.json in jquery-ajax..

http://tylerfrankenstein.com/code/android-app-with-drupal-7-services-phonegap-and-jquery-mobile

Sign up to request clarification or add additional context in comments.

2 Comments

Welcome to Stack Overflow! While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
This tutorial also has not yet been updated to account for CSRF tokens now required by the Services module 3.4 and above.
1

Your ajax URL should be something like this instead:

http://www.myweb.com/my_services_path/user/login.json

Try something like this instead:

$('#page_login_submit').live('click',function(){

  // grab form input
  var name = $('#page_login_name').val();
  if (!name) { alert('Please enter your user name.'); return false; }
  var pass = $('#page_login_pass').val();
  if (!pass) { alert('Please enter your password.'); return false; }

  // make the services call
  var data_string = 'username=' + encodeURIComponent(name);
  data_string += '&password=' + encodeURIComponent(pass);
  $.ajax({
      url: "http://example.com/?q=my_services_path/user/login.json",
      type: 'post',
      data: data_string,
      dataType: 'json',
      error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert('page_login_submit - failed to login');
        console.log(JSON.stringify(XMLHttpRequest));
        console.log(JSON.stringify(textStatus));
        console.log(JSON.stringify(errorThrown));
      },
      success: function (data) {
        alert('You are now logged in!');
      }
  });
  return false;
});

3 Comments

This is a good start but it doesn't handle CSRF tokens now required by services nor does it show how to handle requests after login.
The CSRF token is not needed for User Login. CSRF tokens are only needed for authenticated users doing POST, PUT or DELETE calls. The question did not ask how to handle requests after login. There is information about how to handle the need for the CSRF token here: drupal.org/node/2013781
Great question !!! I was into this brick wall myself. Great answers. Harsh has a good link there too. I am actually pulling through an implementation for my project without too much pain thanks to this post. Thanks, all upboats for everyone, I'm paying off credits to all contribs. for time saved> :) Each response is informative, great overall post & comment threads so you all deserve the love.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.