bgh bgh - 5 months ago 100
jQuery Question

SOAP web service calls from Javascript

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?

bgh bgh
Answer

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?