admin管理员组

文章数量:1312699

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 ().
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 ing 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 .sun.xml.ws.transport.http.server.WSHttpHandler handleExchange WARNING: Cannot handle HTTP method: OPTIONS

I've also tried using a javascript library, soapclient.js (.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?

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./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 ing 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 .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./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?

Share Improve this question edited Mar 24, 2011 at 9:31 gideon 19.5k11 gold badges74 silver badges114 bronze badges asked Mar 24, 2011 at 9:22 bghbgh 2,1702 gold badges30 silver badges39 bronze badges 1
  • 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 Commented Mar 24, 2011 at 11:11
Add a ment  | 

3 Answers 3

Reset to default 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 e 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?

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 pare them.

It might help you understand what is wrong.

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

it's marked as invalid
http://bugs.jquery./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.

本文标签: javaSOAP web service calls from JavascriptStack Overflow