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

Adding 'nil' parameters #36

Closed Sergiodiaz53 closed 10 years ago

Sergiodiaz53 commented 10 years ago

Hi,

Because of my web service I need to send the nil parameters like this:

< q0:data xsi:nil="true"/ >

instead of

< q0:data> < /q0:data >

I have been looking in your code and trying to do some changes in it without results. Could you tell me where I should start modifying? I thought it was in the 'toString()', this is what you send to the ws, right?

Thanks in advance.

doedje commented 10 years ago

I am afraid you message lost some of the more important parts...

are you specifying your data as XML or JSON? For JSON when you have:

{ value: null }

this will be send as:

<value nil="true"></value>

Note the null is specified WITHOUT quotes!

Sergiodiaz53 commented 10 years ago

Sorry. I fixed it.

I'm creating my data using a function(SOAPObject). I'm adding it with .addParamter, like this:

var input = execute.newChild("q0:inputList") input.addParameter("q0:data", inputList[x][0])

inputlist is: var parameter = new Array("P1", "P2", "P3","P4", null, null, "P7", "P8" ) var inputList = new Array(parameter);

may I combine create data with function and JSON?

Thanks.

doedje commented 10 years ago

The short solution for you is to do something like:

if (inputList[x][0] !== null) {
   input.addParameter("q0:data", inputList[x][0])
} else {
   input.newChild("q0:data").attr("xsi:nil","true");
}

In the long term I think I could do a check like this in .addParameter() but then it should also done in .val(value) and that might need some extra testing...

I hope the solution above works for now?

Sergiodiaz53 commented 10 years ago

Perfectly! Thank you very much!!

doedje commented 10 years ago

Since I was already busy writing a reply to your other issue I continued in the same vibe and fixed this issue too!

$.soap({
    data: function(SOAPObject) {
        var x = new SOAPObject('test');
        x.addParameter('val','1').addParameter('null', null).addParameter('undefined');

console.log(x)
console.log(x.find('val').val())
console.log(x.find('null').val())
console.log(x.find('undefined').val())

    }
});

will produce on the console:

<test><val>1</val><null xsi:nil="true"></null><undefined></undefined></test> { typeOf="SOAPObject", name="test", ns={...}, more...}
1
null
undefined