从 javascript 调用 java web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16170429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
call java web service from javascript
提问by madas
I would like to call my webservice methods from pure java script code. and that code should work on mozilla browser.
我想从纯 java 脚本代码调用我的 web 服务方法。并且该代码应该适用于 mozilla 浏览器。
This is my webservice code:
这是我的网络服务代码:
package com.example.core;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class Area {
@WebMethod
public double square(@WebParam(name="side") double side)
{
return side * side;
}
@WebMethod
public double rectangle(@WebParam(name="length") double length,@WebParam(name="breadth") double breadth)
{
return length * breadth;
}
public static void main(String[] args) {
Area area = new Area();
String url = "http://localhost:8090/area"; // end point of webservice.
System.out.println(url+"?wsdl");
Endpoint.publish(url, area); // publishing the webservice
}
}
Here is my HTML file:
这是我的 HTML 文件:
<html>
<head>
<meta content="utf-8" http-equiv="encoding">
<meta content="text/xml;charset=utf-8" http-equiv="Content-Type">
<script language="javascript">
function call()
{
var side = sideid.value;
var side1 = sideid1.value;
var req = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://core.codon.com/\"><soapenv:Body><web:rectangle><length>" + side+ "</length><breadth>" + side1+ "</breadth></web:rectangle></soapenv:Body></soapenv:Envelope>";
//var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
//var reqXML = xmlDoc.loadXML(req);
var xmlDoc=document.implementation.createDocument("", "", null);
xmlDoc.async=false;
xmlDoc.onload = req;
//var reqXML = xmlDoc.load(req);
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4)
{
var response = xmlhttp.responseXML;
alert(response.selectSingleNode(".//return").text);
alert("======"+response);
}
}
var soapaction = "http://core.example.com/rectangle";
xmlhttp.open("POST","http://localhost:8090/area?wsdl",true);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction", soapaction);
xmlhttp.send(req);
}
</script>
</head>
<body>
Side Length: <input type="text" id="sideid"></input>
Length: <input type="text" id="sideid1"></input>
<button onclick="call();">area of square</button>
</body>
</html>
with the above code am getting response as null. The same code working on IE but not in mozilla...
上面的代码得到响应为空。相同的代码适用于 IE 但不适用于 mozilla ......
my webservice side am getting the following error
我的网络服务端收到以下错误
com.sun.xml.internal.ws.transport.http.server.WSHttpHandler handleExchange
WARNING: Cannot handle HTTP method: OPTIONS
Please Help me out..Thanks in advance
请帮帮我..提前致谢
回答by Piotr Kochański
I would take SOAP UI, generate web service client, generate example requests, so I don't need to create SOAP envelopes from scratch. Then I would use jQuery to generate AJAX requests with the help of generated SOAP envelopes.
我会使用 SOAP UI,生成 Web 服务客户端,生成示例请求,所以我不需要从头开始创建 SOAP 信封。然后我将使用 jQuery 在生成的 SOAP 信封的帮助下生成 AJAX 请求。
Another approach would be to make use of http://cxf.apache.org/docs/javascript-clients.html- you will have complete JavaScript generated that way.
另一种方法是使用http://cxf.apache.org/docs/javascript-clients.html- 您将通过这种方式生成完整的 JavaScript。
回答by Nitish
You are running webservice as stand alone app on port say 'x' and the client might be on another port say 'y' When you do a post call through y onto x the method will always be changed to options automatically. Internet standards wont allow 'posting' between different servers, I guess. You will have to find another workaround.
您在端口上将 webservice 作为独立应用程序运行,例如“x”,而客户端可能在另一个端口上,例如“y”当您通过 y 对 x 进行后期调用时,该方法将始终自动更改为选项。我猜互联网标准不允许在不同服务器之间“发布”。您将不得不寻找另一种解决方法。