2

我想从纯 java 脚本代码中调用我的 web 服务方法。并且该代码应该可以在 Mozilla 浏览器上运行。

这是我的网络服务代码:

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
}
}

这是我的 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>

使用上面的代码,我得到的响应为空。相同的代码在 IE 上工作,但在 mozilla 中没有......

我的网络服务端收到以下错误

 com.sun.xml.internal.ws.transport.http.server.WSHttpHandler handleExchange
 WARNING: Cannot handle HTTP method: OPTIONS

请帮帮我..提前谢谢

4

2 回答 2

1

我会使用 SOAP UI,生成 Web 服务客户端,生成示例请求,所以我不需要从头开始创建 SOAP 信封。然后我会使用 jQuery 在生成的 SOAP 信封的帮助下生成 AJAX 请求。

另一种方法是使用http://cxf.apache.org/docs/javascript-clients.html - 您将通过这种方式生成完整的 JavaScript。

于 2013-04-23T16:14:32.630 回答
0

您正在将 web 服务作为独立应用程序运行在端口说“x”上,并且客户端可能在另一个端口上说“y”当您通过 y 对 x 进行发布调用时,该方法将始终自动更改为选项。我猜,互联网标准不允许在不同服务器之间“发布”。您将不得不找到另一种解决方法。

于 2014-01-21T07:04:55.487 回答