Hi, I have developed a webservice in java using NetBeans IDE and tested it using the IDE generated service tester. Now I need to call the web service from javascript. I am using the following code:
var params = new Array();
params[0] = new SOAPParameter(“peter”,“username”);
params[1] = new SOAPParameter(“paul”,“password”);
var soapCall = new SOAPCall();
soapCall.transportURI = ‘http://localhost:8081/TestApplication/TestWSService’;
soapCall.encode(0, “loginUser”, ‘http://test/’, 0, null, params.length, params);
var response = new SOAPResponse();
response = soapCall.invoke();
alert(response.body.textContent);
}[/INDENT][/I]
This method is supposed to return true or false. However, it returns a NullPointerException. This exception is repeated for every Webmethod requiring some form of input parameter. However, when I call a test method “getTime” (which need no input), the response is correct.
Does anyone have any idea what could be the problem?
Alternatively, is there any other way of calling web services?
Answering this question would save my life!! Thanks
I am afraid I have not expressed myself well enough. It is the method of the webservice that is supposed to return true or false. The above method is just calling the webservice with some parameters and outputting the result via the alert().
Could you very kindly direct me to relevant resources about javascript and webservices, from oopschool.com? Thanks a lot.
Thanks for your comments, but my problem is not using javascript or webservices but specifically calling a java webservice I have developed from javascript. I have found numerous resources about using .NET webservices from Javascript but none about Java webservices. And unless I am missing the whole point, there are some differences between the two.
Do you use classes to do the job?. JavaScript objects can communicate with JavaScript objects, but I do not know how you should call a Java Class / object from (a) JavaScript (object) unless there is a pre processor or interpreter?
I stop there and leave it to other members that have understood your problem better.
It is not so easy to see when you are in a hurry. When I sat down and took a break, I finally think I understand your problem. A webservice can of course be delivered as a PHP or a Java file that produces XML code like:
I have not seen such a parser. May be you have to write it yourself.
But it should not be difficult to use a JavaScript web service from a library that I mentioned above on the produced XML file pointing it to the relevant URL.
Have you thought about using Firebug or something like it to track down what exactly is being sent and received? You know, the raw XMLs being transmitted… it seems either JavaScript is not assembling the call properly, or you’re not fetching the output properly. In either case, you should see if the call is even invoked, and if not, track down line by line what could be wrong.
Note also that Native SOAP support has been dropped in Firefox 3 accoding to this note.
If you end up not using SOAP, the other way is to use XMLHttpRequest() (or AJAX if you prefer) to connect to a local PHP/JSP/whatever file that is going to invoke the SOAP call instead of JavaScript. You can use the POST or GET methods to actually send parameters to the script.