简体   繁体   English

来自Javascript的SOAP Web服务调用

[英]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. 我正在努力从网页成功地对SOAP Web服务进行Web服务调用。 The web service is a Java web service that uses JAX-WS. 该Web服务是使用JAX-WS的Java Web服务。

Here is the web method that I'm trying to call: 这是我要调用的Web方法:

@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 ). 我尝试使用JQuery库jqSOAPClient( http://plugins.jquery.com/project/jqSOAPClient )进行Web服务调用。
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'. jqSOAPClient.js时,我记录了xData.responseXML数据成员,但未定义。 In the web service I see the warning 在Web服务中,我看到警告

24 Mar 2011 10:49:51 AM com.sun.xml.ws.transport.http.server.WSHttpHandler handleExchange WARNING: Cannot handle HTTP method: OPTIONS 2011年3月24日,上午10:49:51 com.sun.xml.ws.transport.http.server.WSHttpHandler handleExchange警告:无法处理HTTP方法:选项

I've also tried using a javascript library, soapclient.js ( http://www.codeproject.com/kb/Ajax/JavaScriptSOAPClient.aspx ). 我也尝试使用JavaScript库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). 因为它不起作用,所以我绕过了soapclient.js中获取WSDL的部分(我得到了: IOException: An established connection was aborted by the software in your host machine Web服务端IOException: An established connection was aborted by the software in your host machine上的IOException: An established connection was aborted by the software in your host machine )。 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. 仅为使用的适当名称空间检索WSDL,因此我仅将变量ns替换为实际的名称空间。

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". 我在Web服务上收到与以前完全相同的警告(无法处理HTTP方法:OPTIONS),并且在浏览器的错误控制台中收到错误“文档为空”。 When I log the value of req.responseXML in soapclient.js I see that it is null. 当我在soapclient.js中记录req.responseXML的值时,我看到它为空。

Could anyone advise on what might be going wrong and what I should do to get this to work? 任何人都可以就可能出什么问题以及如何使它正常工作提出建议吗?

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 . 这与该线程中的场景相同: jQuery $ .ajax(),$ .post在Firefox中以REQUEST_METHOD的形式发送“ OPTIONS”

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. 基本上,我使用的是Firefox,当使用AJAX从Firefox进行跨域调用(Web服务的地址域与网页的域不同)时,Firefox首先发送OPTIONS HTTP消息(在它发送POST消息之前),从Web服务确定是否应允许该呼叫。 The web service must then respond to this OPTIONS message to tell if it allows the request to come through. 然后,Web服务必须响应此OPTIONS消息,以告知其是否允许请求通过。

Now, the warning from JAX-WS ("Cannot handle HTTP method: OPTIONS") suggests that it won't handle any OPTIONS HTTP-messages. 现在,来自JAX-WS的警告(“无法处理HTTP方法:OPTIONS”)表明它不会处理任何OPTIONS HTTP消息。 That's ok - the web service will eventually run on Glassfish. 没关系-该Web服务最终将在Glassfish上运行。 The question now is how I can configure Glassfish to respond to the OPTIONS message. 现在的问题是我如何配置Glassfish来响应OPTIONS消息。

In the thread referenced above Juha says that he uses the following code in Django: 在上面引用的线程中,Juha说他在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. Access-Control-Allow-Origin提供了一个模式,该模式指示将接受哪些来源(收件人地址)(我的可能比仅允许任何来源更严格),并且Access-Control-Max-Age在几秒钟后告诉客户端将不得不再次请求许可。

How do I do this in Glassfish? 如何在Glassfish中做到这一点?

Have you actually tested that ws is working properly? 您是否已经实际测试过ws正常工作?
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. 您可以使用SoapUI检查请求/响应等。当您确认ws正在SoapUI中工作时,请检查原始Soap消息的格式是什么。 Then try to inspect how it looks before sending with .js method, and compare them. 然后尝试检查外观,然后再使用.js方法发送并进行比较。

It might help you understand what is wrong. 它可以帮助您了解问题所在。

Check if this helps 检查是否有帮助
http://bugs.jquery.com/attachment/ticket/6029/jquery-disable-firefox3-cross-domain-magic.patch 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 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. 另一方面,如果您可以创建并调用将向ws请求并返回结果的本地页面,则覆盖跨域脚本的正确设置可能更好。
Or even better, you can create page that will receive url as param and do request to that url and just return result. 甚至更好的是,您可以创建一个页面,该页面将接收url作为参数,并请求该url并仅返回结果。 That way it will be more generic and reusable. 这样,它将更加通用和可重用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM