JavaScript - Node.Save & Edit // Cross-method Insights

Last updated on
30 April 2025

// #### JavaScript - Node.Save & Edit // Cross-method Insights

// ## For EDIT info refer to the code comments in node_object


// Releases
// // # SERVICES 6.x-2.0 and JSON SERVER 6.x-2.0-alpha1

// Most of this would apply to older versions as well

// // If your Services server uses the drupal_to_js() method 
   // // Or if you are generally having problems with processing your node object
   // // You may need to serialize (make a text string) your node object, or other deep structs / arrays
   // // I've made notes below in "**Serialization Issues**"

// + + Node Object


var date = new Date();
var timestamp = Math.round(date.getTime() / 1000);

var node_object = {
    "type": "story",
    "nid": 0,  // Set value to 0 for node.save. Use nid (int) for node.update
    "uid": 1,  
    "name": "admin",
    "title": "My First Node",
    "body": "Hello World!",
    "created": timestamp, // Set key to 'created' for node.save. Use 'changed' for node.update 
    "status": 1 // Set to 1 for Published. O for unpublished.
};


  // **Serialization Issues**

  // Some Services servers (such as older versions of the JSON Server module, SWX PHP) have poor parsing capabilities that fail on deep structs or arrays
  // You may get a hair-pulling mystery error that boils down to one simple solution:
  // // You need to convert your node_object to a JSON or Serialized PHP string before calling the method

  // For example, with JSON:
  // // var object = { "foo": "bar" }; 
  // // Should be:  var object_string = "{\"foo\":\"bar\"}';

  // To do this automatically, using MooTools, I'd use this method:

  // // node_object = JSON.encode(node_object);

  // If you're using Flash or working with Serialized PHP, I recommend reviewing this thread
  // // http://groups.drupal.org/node/66478


// CCK Fields

// Reference: JavaScript - CCK Fields in the Node Object (applies to any language)
// http://drupal.org/node/776122

// + + Data Object


// This data object is the instruction between your Drupal Services Server
// // Example of how to implement this is in the next code box below

var data: { 
  "method": "node.save", 
  "node": node_object
}; 

// #### Calling the Request


// The Following examples are for MooTools
// // Whether you use jQuery or Flash/Flex, etc, you'll have similiar methods

JSON
http://drupal.org/node/522942

JSONP
http://drupal.org/node/791922


// #### Reference: Services & JavaScript

JavaScript - Node.Save & Edit // Cross-method Insights
http://drupal.org/node/774116

JavaScript - CCK Fields in the Node Object // Cross-method Insights
http://drupal.org/node/776122

JavaScript - Comment.Save
http://drupal.org/node/788892

JavaScript - JSON Server // Request.JSON (MooTools Example)
http://drupal.org/node/522942

JavaScript - JSON Server // Request.JSONP (MooTools Example)
http://drupal.org/node/791922

Help improve this page

Page status: Not set

You can: