Besides cURL, the JSON server may also be accessed via javascript.

JSON module comes with the json_server.js file that provides a method to access the server via Javascript. The JSON server module provides the json_load() function that simplifies the inclusion of the json_server.js file at page render time.

Drupal.service('service.name', 
  {api_key: "123461823762348756293", sessid: "sdfjahsldjfhlaksuertybsi", extra_parameter: "asdfasdf", other_parameter: ["nodes", "are", "good"]}, 
  function(status, data) {
    if(status == false) {
      alert("FATAL ERROR!!!!!");
    }
    else {
      alert(data);
    }
  }
);

Drupal.service takes three parameters: service name, parameters, and callback function.
The callback function accepts two parameters returned by the service - the success/failure of the request and data.
Data is usually an object, and if the success if false, it contains a useful message.

This can be very useful for debugging services or even using JSON server for ajax callbacks.

Consider an the following example that would call the node.load service using the JSON server. Upon clicking a link, a call is made to the service and upon success the contents of the node are inserted into the page.

$('a.classname').bind('click', function() {
  Drupal.service('node.load',
    {nid: 2, fields: ["nid", "title", "body"]},
    function(status, data) {
      if(status == false) {
        alert("Fatal error: could not load content");
      }
      else {
        $('#content_area').html("<h1><a href=\""+ data.nid + "\">"+ data.title +"</a></h1><br /><p>"+ data.body +"</p>");
      }
    }
  );
}

Comments

mirian’s picture

Miriam N.

It's seemed fantastic but did not work for me...
the line "Drupal.service" failed said "the object doesn't support this property or method".
What is the problem?

mukhsim’s picture

For D6, use node.get instead of node.load:

$('a.classname').bind('click', function() {
  Drupal.service('node.get',
    {nid: 2, fields: ["nid", "title", "body"]},
    function(status, data) {
      if(status == false) {
        alert("Fatal error: could not load content");
      }
      else {
        $('#content_area').html("<h1><a href=\""+ data.nid + "\">"+ data.title +"</a></h1><br /><p>"+ data.body +"</p>");
      }
    }
  );
}

Delf.

yajnin’s picture

Today I worked through connecting to Services from JavaScript (using MooTools as my framework). Connecting was fairly simple, I've posted the MooTools protocol here:

http://groups.drupal.org/node/24372

Further, I discovered difficulty in posting a JSON string as an argument via JSON server. The solution was to escape the string using one or two methods I've discussed here:

http://groups.drupal.org/node/24372#comment-84315

// Good luck MooToolers (and all JavaScripters alike). I'm having an incredible time working with Drupal and Services. Now with MooTools?? * Pumps Fist *

noobizness’s picture

I'm attempting to call the method and its having problems with the 'api_key' parameter: its not in the $arg_dict and therefore throws an "Unknown named parameter 'api_key' received" error. (D6, using api keys)

cheewe’s picture

It appears that JSON server module is not supported in Services 3.x, which claims it has its own JSON support.

What is the migration path of this code to Services 3.x? Does someone have an equivalent javascript library that allows Drupal.service('node.get' call to be made?

Thanks!