English 中文(简体)
Drupal node.save and JSONP
原标题:

I am having an issue with call Drupal node.save using MooTool s JSONP. Here is an example.

Here is my request:

callback Request.JSONP.request_map.request_1
method node.save
sessid 123123123123123
node {"type":"blog","title":"New Title","body":"This is the blog body"}

Here is my result

HTTP/1.0 500 Internal Server Error

I got this working before, but i used AMFPHP and was able to send objects to drupal. I am assuming that this has to do with Drupal expecting an object, but since it is a GET it gets transformed as a string. Is there any way of getting around this with out hacking the code?

Here is my code:

$( newBlogSubmit ).addEvent( click , function()
{
    var node = {
        type : "blog",
        title:"New Title",
        body :"This is the blog body"
    }

    var string = JSON.encode(node);
    string.escapeRegExp()

    var sessID = _sessID;

    DrupalService.getInstance().node_save(string, sessID, drupal_handleBlogSubmit);
});

My Drupal Service JS Code:

//NODE
DrupalService.prototype.node_save = function(node, sessid, callback){
    var dataObj = {
        method : "node.save",
        sessid : sessid,
        node : node
    }
    DrupalService.getInstance().request(dataObj, callback);
}


//SEND REQUEST AND CALLBACK FUNCTION

DrupalService.prototype.request = function(dataObject, callback){
    new JsonP( http://myDrupalSite.com/services/json , {data: dataObject,onComplete: callback}).request();
}

I am trying to connect the dots, but not too familiar with Drupal, but i would guess all I need to do is turn the string back into an object. Any ideas where I should be looking, or if there is an existing patch?

问题回答

A first question could be why you use mootools since Drupal comes with jQuery and use it extensively throughout the different modules and Drupal core itself.

Anyways I don t know mootools so can t help you there, but if your request in ending in a internal server error, you have a problem with your drupal code or your js code. So even if I knew exactly what you were doing, I couldn t tell you the problem without looking at the drupal code for your http://myDrupalSite.com/services/json callback.

In general, what you want to make sure is:

  • You make a POST request, as drupal will cache get s and the semantic of this, is that you are posting data - the node - to the server.
  • Your data should be sent as post params, this will make them end up in the PHP $_POST variable
  • Your callback should validate the data and act accordingly, creating a node when the data is intact. You don t need session id s since the script will have the same session the browser has.

I ve answered a similar question in detail, which was about altering a field instead of saving a node, but much of the work is still the same. You can take a look on the post, although this is with jQuery and not Mootools.





相关问题
Drupal Multi-language: Simple strings not translated

I m adding additional languages to a Drupal site that I m building. Getting the translation of content working is fairly easy using the Internationalisation module. Yet, simple things such as date ...

Setting up a WYSIWYG editor for Drupal site users [closed]

Looking through the Drupal contrib modules, and after a few Google searches, it becomes evident that there are any number of choices and combos available to set up a WYSIWYG editor in Drupal. I m ...

Change size of user/password login box

I don t know how to change the size of the login username/password boxes on the drupal site that I m trying to build. I m stumbling through the theming, and don t know where to find the file that ...

How does Drupal provide an edit/review/publish model?

How does Drupal support a means to update and review a website before it is published? Does it only allow you to preview a page at a time before you publish it or is there a way to create a site ...

Term for rotating header

I m looking for terminology that describes this behavior: The header of a web-page contains a different image every time you visit it. Update: It is not an advertisement, but images related to the ...

Has anyone checked out Drupal 7? [closed]

Has anyone checked out a copy of Drupal 7 yet? What do people think? I m pretty excited about the PDO and all of the designers I work with a very excited about the new admin interface/structure. Do ...

热门标签