CalculadoraWebService:
package in.gruporia.javawebservice;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "CalculadoraWebService")
public class CalculadoraWebService {
//Retorna la SUMA de dos numeros enteros
@WebMethod(operationName = "AddIntegers")
public int add(@WebParam(name = "firstNum") int num1, @WebParam(name = "secondNum") int num2) {
return num1 + num2;
}
//Retorna la RESTA de dos numeros enteros
@WebMethod(operationName = "SubIntegers")
public int sub(@WebParam(name = "firstNum") int num1, @WebParam(name = "secondNum") int num2) {
return num1 - num2;
}
//Retorna el PRODUCTO de dos numeros enteros
@WebMethod(operationName = "MulIntegers")
public int mul(@WebParam(name = "firstNum") int num1, @WebParam(name = "secondNum") int num2) {
return num1 * num2;
}
//Retorna la DIVISION de dos numeros enteros
@WebMethod(operationName = "DivideIntegers")
public int div(@WebParam(name = "firstNum") int num1, @WebParam(name = "secondNum") int num2) {
return num1 / num2;
}
}
的index.html
<html>
<head>
<title>UseSwap</title>
<script>
var service;
function InitializeService(){
service.useService("http://localhost:8080/Calculadora/CalculadoraWebService?wsdl", "CalculadoraWebService");
}
var num1, num2, result;
function Add(){
num1 = document.DemoForm.Numero1.value;
console.log(num1);
num2 = document.DemoForm.Numero2.value;
console.log(num2);
result = service.CalculadoraWebService.callService("add", num1, num2);
console.log(result);
alert(event.result.value);
}
</script>
</head>
<body onload="InitializeService()" id="service"
style="behavior:url(webservice.htc)">
<form name="DemoForm">
Numero 1 : <input type="text" name="Numero1"/>
Numero 2 : <input type="text" name="Numero2"/>
<button onclick="Add()">Resultado</button>
</form>
</body>
</html>
CalculadoraWebService运行正常,我在预览练习中使用java消耗它,调用所有方法但现在我已经使用javascript来完成它。 num1和num2值很好,但出现错误
SCRIPT5007:无法获得财产&#39; callService&#39;未定义或空引用
答案 0 :(得分:1)
我相信你有一个范围问题,在初始化它的函数之外定义service
作为var,这里是片段:
var service; //add this
function InitializeService(){
service.useService("http://localhost:8080/Calculadora/CalculadoraWebService?wsdl", "GetSumService");
}
如果你不在函数service
之外声明InitializeService
,Js会在该函数范围内声明该变量,然后当你尝试在Add函数中使用它时,该范围是函数将看不到service
var。