2012-04-29

Web services(soap). This takes me back to 2005, in a project where we thought it was the solution for all the problems.
How wrong were we?

But to be fair, it is not all that bad. I remember back in 2004 when I did a college coursework consuming a .net webservice with a java application. The idea was awesome, but it was so painful I thought it would never be any good.
Now I had to do another coursework. Not for college degree, but anyway I had to build a website, and then convert it to a web service.
But this time I thought: I don't want to build another project to consume it, it will be just a waste of time.

Solution: JQuery. Yes, the solution for so many web questions is the answer once again...

And it was quite easy.
You just have to send it as xml, and build a xml envelope to send as data. Sounds hard?
Well, you will see how easy it is.
To build the envelope all you have to do is to access the webservice you generated, in my case Java.
As you can see, the SOAP request is already done for you, it's only a matter of copy and replace the values.
var soapMessage = '<?xml version="1.0" encoding="UTF-8"?>'
                    + '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Header/><S:Body>'
                    + '    <ns2:listCustomers xmlns:ns2="http://webservice/">'
                    + '    </ns2:listCustomers>'
                    + '</S:Body></S:Envelope>';
                $.ajax({
                    type: "POST",
                    url: "./WSManager",
                    data: soapMessage,
                    contentType: "text/xml; charset=\"utf-8\"",
                    success: function(msg) { $("#customerList").html(msg.childNodes[0].textContent); }
                });


But the thing is, why to use soap?
Seriously, I can't see any reason in that. REST is so much better, is clean, nice, you can have it with json instead of the verbose xml, you don't have to send a whole xml as request.

I'll write some post about rest later, because now it is late and I've got to sleep.

see ya