0

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>

The CalculadoraWebService works fine, I consumed it with java in a previews excercise, calling all the methods but now I've to do it using javascript. num1 and num2 values are fine, but an error appears

SCRIPT5007: Unable to get property 'callService' of undefined or null reference

Nick
  • 1,417
  • 1
  • 14
  • 21

1 Answers1

1

I believe you have a scope problem, define service as a var outside the function that initializes it, here is the snippet:

var service; //add this
function InitializeService(){
    service.useService("http://localhost:8080/Calculadora/CalculadoraWebService?wsdl", "GetSumService");
}

If you dont declare service outside the function InitializeService, Js will declare that variable inside the scope of that function, and then when you try to use it in your Add function, the scope of that function won't see the service var.

taxicala
  • 21,408
  • 7
  • 37
  • 66
  • It continues giving me this error `Unable to get property 'GetSumService' of undefined or null reference` – Valentina Troche Jun 11 '15 at 18:38
  • Then you must have a definition of service somewhere else, is this your full code? – taxicala Jun 11 '15 at 18:40
  • I still believe there some JS code missing, I dont see where you declare `service` in your code (without my answer, of course). `service` must be being declared somewhere in your code, I need to see that code. – taxicala Jun 11 '15 at 18:50
  • I did not mean that You should add my code, It seems that service is being declared previously than the JS code you've shared. – taxicala Jun 11 '15 at 19:32