8

I'm struggling to successfully make a web service call to a SOAP web service from a web page. The web service is a Java web service that uses JAX-WS.

Here is the web method that I'm trying to call:

@WebMethod  
public String sayHi(@WebParam(name="name") String name)  
{  
    System.out.println("Hello "+name+"!");  
    return "Hello "+name+"!";  
}

I've tried doing the web service call using the JQuery library jqSOAPClient (http://plugins.jquery.com/project/jqSOAPClient).
Here is the code that I've used:

var processResponse = function(respObj)  
{  
    alert("Response received: "+respObj);  
};

SOAPClient.Proxy = url;  
var body = new SOAPObject("sayHi");  
body.ns = ns;  
body.appendChild(new SOAPObject("name").val("Bernhard"));

var sr = new SOAPRequest(ns+"sayHi",body);  
SOAPClient.SendRequest(sr,processResponse);

No response seems to be coming back. When in jqSOAPClient.js I log the xData.responseXML data member I get 'undefined'. In the web service I see the warning

24 Mar 2011 10:49:51 AM com.sun.xml.ws.transport.http.server.WSHttpHandler handleExchange WARNING: Cannot handle HTTP method: OPTIONS

I've also tried using a javascript library, soapclient.js (http://www.codeproject.com/kb/Ajax/JavaScriptSOAPClient.aspx). The client side code that I use here is

var processResponse = function(respObj) 
{
    alert("Response received: "+respObj);
};

var paramaters = new SOAPClientParameters();
paramaters.add("name","Bernhard");
SOAPClient.invoke(url,"sayHi",paramaters,true,processResponse);

I've bypassed the part in soapclient.js that fetches the WSDL, since it doesn't work (I get an: IOException: An established connection was aborted by the software in your host machine on the web service side). The WSDL is only retrieved for the appropriate name space to use, so I've just replaced the variable ns with the actual name space.

I get exactly the same warning on the web service as before (cannot handle HTTP method: OPTIONS) and in the browser's error console I get the error "document is null". When I log the value of req.responseXML in soapclient.js I see that it is null.

Could anyone advise on what might be going wrong and what I should do to get this to work?

gideon
  • 19,329
  • 11
  • 72
  • 113
bgh
  • 1,986
  • 1
  • 27
  • 36
  • When I deploy the web service unto Glassfish, the client gets the same responses ("undefined" in the case of jqSOAPClient.js and null when using soapclient.js). – bgh Mar 24 '11 at 11:11

3 Answers3

2

I found out what was going on here. It is the same scenario as in this thread: jQuery $.ajax(), $.post sending "OPTIONS" as REQUEST_METHOD in Firefox.

Basically I'm using Firefox and when one is doing a cross domain call (domain of the address of the web service is not the same as the domain of the web page) from Firefox using AJAX, Firefox first sends an OPTIONS HTTP-message (before it transmits the POST message), to determine from the web service if the call should be allowed or not. The web service must then respond to this OPTIONS message to tell if it allows the request to come through.

Now, the warning from JAX-WS ("Cannot handle HTTP method: OPTIONS") suggests that it won't handle any OPTIONS HTTP-messages. That's ok - the web service will eventually run on Glassfish. The question now is how I can configure Glassfish to respond to the OPTIONS message.

In the thread referenced above Juha says that he uses the following code in Django:

def send_data(request):  
    if request.method == "OPTIONS":   
        response = HttpResponse()  
        response['Access-Control-Allow-Origin'] = '*'  
        response['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'  
        response['Access-Control-Max-Age'] = 1000  
        response['Access-Control-Allow-Headers'] = '*'  
        return response  
    if request.method == "POST":  
        # ... 

Access-Control-Allow-Origin gives a pattern which indicates which origins (recipient addresses) will be accepted (mine might be a bit more strict than simply allowing any origin) and Access-Control-Max-Age tells after how many seconds the client will have to request permission again.

How do I do this in Glassfish?

Community
  • 1
  • 1
bgh
  • 1,986
  • 1
  • 27
  • 36
  • I've decided to avoid the cross-domain web service call by running a proxy PHP web service on the server on which the web site is hosted. The Javascript calls this PHP web service and the PHP script forwards the requests to the target web service. – bgh Apr 06 '11 at 13:11
1

Have you actually tested that ws is working properly?
You can use SoapUI for inspecting request/response etc. When you confirm that ws is working from SoapUI, inspect what is format of raw Soap message. Then try to inspect how it looks before sending with .js method, and compare them.

It might help you understand what is wrong.

Aleksandar
  • 181
  • 2
  • 11
  • Thank you for the suggestion. The web service works fine from soapUI. I've compared the contents of the request that is sent by soapUI against what is sent by jqSOAPClient. – bgh Mar 24 '11 at 12:45
  • The requests are given below: soapUI: ` Bernhard ` jqSOAPClient: ` Bernhard ` – bgh Mar 24 '11 at 12:45
  • There are minor differences, but I believe they're both correct. I've taken the request that soapUI sends and hard-coded it into jqSOAPClient.js so that it sends exactly that string but the web service still gives the warnings `24 Mar 2011 2:36:32 PM com.sun.xml.ws.transport.http.server.WSHttpHandler handleExchange WARNING: Cannot handle HTTP method: OPTIONS` and no response. The web method does execute when I use soapUI, but it is not at all run when called from the web page. – bgh Mar 24 '11 at 12:47
0

Check if this helps
http://bugs.jquery.com/attachment/ticket/6029/jquery-disable-firefox3-cross-domain-magic.patch

it's marked as invalid
http://bugs.jquery.com/ticket/6029
but it might give you some hint

On the other hand, instead to override proper settings for cross-domain scripting might be better if you can create and call local page that will do request to ws and return result.
Or even better, you can create page that will receive url as param and do request to that url and just return result. That way it will be more generic and reusable.

Aleksandar
  • 181
  • 2
  • 11