我想从纯 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
请帮帮我..提前谢谢