doedje / jquery.soap

This script uses $.ajax to send a SOAP:Envelope. It can take XML DOM, XML string or JSON as input and the response can be returned as either XML DOM, XML string or JSON too.
352 stars 147 forks source link

How to set attributes and parameter namespaces? #29

Closed hpatelc20g closed 10 years ago

hpatelc20g commented 10 years ago

I really like this library, but was wondering if it is possible to set attributes for parameters. For example, the result I want looks like this:

<soap:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">

The cursor parameter has attributes that I want to set. I assume that setting a namespace for a parameter is done similarly. Is it possible to do this with the library as it is? If not, do you know of the simplest way to edit the library to handle parameter attributes? Thanks in advance
doedje commented 10 years ago

This can be done. Note that you cannot specify your parameters (or data as it is called since version 1.3.0) using JSON. JSON is limited in comparison to XML, the latter has native support for attributes, so you'll have two options:

// create your xml as either a string or as XMLDOM object here...
var myXmlWithAttributes = '<GetAllMaterialDetailObjects xmlns:ns="http://www.c20g.org/vms/db/1.0">
<cursor id="0" position="0" numRows="5" maxRows="99999" sameConnection="false" />
</GetAllMaterialDetailObjects>';

$.soap({
  data: myXmlWithAttributes
});

Another way is to use $.soap's SOAPObject. The methods of this object are not yet documented, but when you look at the $.soap code you can find the methods available. When you are familiar with jQuery, you will quickly get the hang of it. When you specify your data as a function you will get back a reference to the SOAPObject as the first parameter of your function:

$.soap({
  data: function(SOAPObject) {
    return new SOAPObject('GetAllMaterialDetailObjects')
      .addNamespace('ns', 'http://www.c20g.org/vms/db/1.0')
        .newChild('cursor')
          .attr('id', 0)
          .attr('numRows', 5)
          .attr('maxRows', 99999)
          .attr('sameConnection', 'false')
        .end(); // this line does the same as the .end() in jQuery, it returns the parent of the current node. If you would omit it the function would return the cursor node, and not the GetAllMaterialDetailObjects node.
  }
});

While the XML string approach is probably the easiest to understand and implement, the choice is yours! =]

hpatelc20g commented 10 years ago

This information helps a lot. I think i will look into using $.soap's SOAPObject. Thanks!