【问题标题】:Can't call a WebMethod using javascript (html)无法使用 javascript (html) 调用 WebMethod
【发布时间】:2015-08-27 14:50:53
【问题描述】:

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

}


索引.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:无法获取未定义或空引用的属性“callService”

【问题讨论】:

    标签: javascript html web-services consuming


    【解决方案1】:

    我相信你有一个范围问题,将service定义为初始化它的函数之外的var,这里是sn-p:

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

    如果你不在函数InitializeService之外声明service,Js会在那个函数的作用域内声明那个变量,然后当你尝试在你的Add函数中使用它时,那个函数的作用域就不会请参阅service var。

    【讨论】:

    • 它继续给我这个错误Unable to get property 'GetSumService' of undefined or null reference
    • 那你肯定在别的地方定义了服务,这是你的完整代码吗?
    • 我仍然认为缺少一些 JS 代码,我看不到您在代码中声明 service 的位置(当然没有我的回答)。 service 必须在您的代码中声明,我需要查看该代码。
    • 我不是说你应该添加我的代码,看来服务比你共享的JS代码更早被声明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    相关资源
    最近更新 更多