Calling web services from javascript

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:

[I][INDENT]function testWebService(){
try {
netscape.security.PrivilegeManager.enablePrivilege(“UniversalBrowserRead”);
} catch (e) {
alert(e);
return false;
}

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

  1. Cryptical post. I assume that the objects are instantiated correctly.
  2. I have coloured potential errores. You call it a method, while I see it as a function and you return from the function may not be correct.
  3. Yes there are many ways of calling web services. You find some resources in my OopSchool link.

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.

I think there are some in the upper right part of the menu starting with “YUI:The Pattern Library”

The simplest is sometimes to use JSON that http://del.icio.us/ uses. I would primarily prefer to use PHP for Web Services.

Do you know the tutorials at http://www.w3schools.com/ that I have linked to on my site.

But I do not know whether they teach JavaScript WebServices.

If I Google

“JavaScript Web Services”

I get 9980 hits.

If I Google

“JavaScript library”

I get 699 000 hits with this

excellent library as the first hit.

Look at what these libraries can offer you.

It should also be possible to make better queries than I have done above
like

“web services” site:jquery.com/

I get 7 hits here in Norway.

Page search (CTRL + F) + Web Services on the following two hits gives specific hits.

http://docs.jquery.com/Tutorials

http://plugins.jquery.com/project/Plugins/category/53

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.

Does that IDE produce XML documents?

Excuse me. Calling a Java webservice from JavaScript. Years since I used Java and C++. To me a web service is an XML file.

Related http://json.org/

More precisely:

  1. Do you mean: call Java code from JavaScript?

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:

http://www.sitepoint.com/rss.php

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.