jquery call to services xmlrpc

Events happening in the community are now at Drupal community events on www.drupal.org.
sime's picture

I'm trying to understand the substance of an xmlrpc call. I've got a plain node with PHP and using drupal_add_js() to get an alert with the callback results.

<?php
$js
= '
  alert(
    $.post("http://example.com/services/xmlrpc",
    "{methodName: node.load; nid: 1}")
  );'
;
drupal_add_js($js, 'inline', 'header');
?>

The alert says "undefined", and as far as i can tell, I'm not getting to the services_server call in the code.

Any ideas?

Comments

Callback

recidive's picture

To make an asynchronous call, you need to register a callback. I.e. a function that will run when the result is returned. The callback is the third argument of $.post() function. Also the parameters (second argument) should be a JS object, not a string.

Try this:

<?php
$js
= '
$.post(
  "http://example.com/services/xmlrpc",
  {methodName: node.load; nid: 1},
  function(data){
    alert("Data: " + data);
  }
);'
;
drupal_add_js($js, 'inline', 'header');
?>

methodName is a string

recidive's picture

Ahh, methodName should be a string, and object properties are separated by commas.

<?php
$js
= '
$.post(
  "http://example.com/services/xmlrpc",
  {methodName: "node.load", nid: 1},
  function(data) {
    alert("Data: " + data);
  }
);
'
;
drupal_add_js($js, 'inline', 'header');
?>

Ah. thankyou. I wouldn't

sime's picture

Ah. thankyou. I wouldn't have worked this out alone. The alert I now get is "Data [object XMLDocument]". I'm now trying to "see" the response. I'm failing with things like data.innerHTML, and data.reponseXML. I wonder if I should be using the $.ajax call so I can interogate the response.

I hope I can add this to the handbook, it would be good to have a quick way to test xmlrpc services.

Shouldn't you...

dmitrig01's picture

be using the JSON server??

yes, apparently I'm a fool

sime's picture

I did get myself very confused, for some reason I was looking at the $.post documentation and using the json parameter in the example as a model. The xmldoc I am getting back is an error :P

So I've worked out the xml input, but still working out how to "see" the xml output (error or not) with the jquery...

How is this done?

Carnix's picture

So -- Googling jQuery, Drupal and XMLRPC, this is the only thing that seems to come up that actually is related to those three things. I'm having a hard time with this one. I have a site that currently uses Flash to pull in data via a View that has a relationship with a Nodequeue. The nodequeue pulls in components form three seperates nodes, that is is exposed to the XMLRPC service via a single view. So... the flash pulls in the data from XMLRPC and does it's thing with it.

I hate Flash, and in this case, Flash isn't useful and I'd like to replace it. (despite my personal loathing, it DOES have it's uses). There are some elements of dynamism involved, but they can all easily be replicated using jQuery. However, I'm having a hard time figuring out exactly how to create the jQuery AJAX call necessary to consume the XMLRPC. It's all done via XML, so no JSON. Would love to see a working example of a jQuery function that can interact with the XMLRPC service.

Best I've been able to do so far is get this response, and nothing I've tried has worked otherwise:

<methodResponse>
  <fault>
  <value>
    <struct>
    <member>
      <name>faultCode</name>
      <value><int>-32700</int></value>
    </member>
    <member>
      <name>faultString</name>
      <value><string>Parse error. Request not well formed.</string></value>
    </member>
    </struct>
  </value>
  </fault>
</methodResponse>

Thanks!

Working PoC

Carnix's picture

So -- of course, a few minutes after posting a question online my repeated head bashing actually paid off! Here is a working proof of concept. The trick was, of course, both obvious and simple (as these things tend to often be). First, you have to disable the processing directive: "processData: false" and then, you pass the XML input (which could be created more efficiently, I did it this way for demonstration) directly as a string (not as an object per se using curly braces). The http response object contains the XML document returned by the XMLRPC service as a text/xml type (and therefore traverseable using standard methods).

var xml_input = "<methodCall>";
xml_input += "<methodName>views.get</methodName>";
  xml_input += "<params>";
   xml_input += "<param>";
    xml_input += "<value>";
     xml_input += "<string>nodequeue_1</string>";
    xml_input += "</value>";
   xml_input += "</param>";
  xml_input += "</params>";
xml_input += "</methodCall>";

$.ajax({
type: "POST",
url: "/services/xmlrpc",
processData: false,
data: xml_input,
success: function(http){
  console.log(http);
}
});

Carnix, why not use the REST

kylebrowning's picture

Carnix, why not use the REST Server for jQuery AJAX calls and ask for the response in json?

Unfortunatly, I Can't

Carnix's picture

I hear you -- you're probably right too. Can't though, this is an existing site that wasn't built by my team (rather, was an external agency that didn't implement it in a particularly good way) and I'm trying to find the best path within the existing architecture (however flawed) to make things work. Changing out whole modules is a long term task, but frankly, if I can allocate resources to do that, I can probably just re-do the thing from the ground up....

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: