【问题标题】:Calling a Specific Web Service Method Using Javascript or jQuery使用 Javascript 或 jQuery 调用特定的 Web 服务方法
【发布时间】:2016-05-09 13:17:45
【问题描述】:

我在加载 jsp 页面时成功调用了 Web 服务。我面临的问题是设置用户能够按下按钮在同一个 Web 服务上调用不同的方法。函数“getSS()”应该完成这个,但我想javascript不会直接读取这种类型的调用。我找到了调用 Web 服务并向其传递参数的解决方案,但这些解决方案通常不会调用 Web 服务中的特定方法。

这是我的代码,我可以在“getSS()”函数中添加什么来完成此操作?我已经加载了 jQuery,并且 web 服务是用 java 编写的。请注意,我已经编写了代码来完成所有工作,我只需要知道如何调用该特定代码。其他解决方案似乎都不符合我的需求。

<%-- 
Document   : index
Created on : May 6, 2016, 9:39:44 AM
Author     : mmarino
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script type="text/javascript">



        function getSS(){
    var e = document.getElementById("names");
    var daString = e.options[e.selectedIndex].value;
    try {
org.me.calculator.CalculatorWS_Service service = new org.me.calculator.CalculatorWS_Service();
org.me.calculator.CalculatorWS port = service.getCalculatorWSPort();
// TODO process result here
java.lang.String result = port.SS(daString);
document.getElementById('inputhere').innerHTML = "hi";
} catch (Exception ex) {
// TODO handle custom exceptions here
    document.getElementById('inputhere').innerHTML = ex;
}
    }
        </script>
</head>
<body>
    <h1>Hello World!</h1>    <%-- start web service invocation --%><hr/>
    <button value="hi" onclick="getSS()"> Click</button>
    <select id="names">
<%
try {
org.me.calculator.CalculatorWS_Service service = new org.me.calculator.CalculatorWS_Service();
org.me.calculator.CalculatorWS port = service.getCalculatorWSPort();
// TODO process result here

java.lang.String result = port.getNames();
out.println(result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
<%-- end web service invocation --%>
    </select>
<hr/>
<div id="inputhere">Hi.</div>
</body>

【问题讨论】:

    标签: javascript java jquery web-services


    【解决方案1】:

    所以看来我所缺少的只是将 java 代码设置为变量并放置正确的开始和结束语句:

      function doIt(){
    <%-- start web service invocation --%>
              var x = <%
    try {
    org.me.testbilling.TestBillingWS_Service service = new     org.me.testbilling.TestBillingWS_Service();
    org.me.testbilling.TestBillingWS port = service.getTestBillingWSPort();
     // TODO initialize WS operation arguments here
    java.lang.String name = "Marcel" ;
    // TODO process result here
    java.lang.String result = port.getSS(name);
    out.println(result);
    } catch (Exception ex) {
    // TODO handle custom exceptions here
    }
    %>
    <%-- end web service invocation --%>
    
        alert(x);
            }
        </script>
    

    仍然不太确定如何将“java.lang.String name =”设置为 javascript 变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      相关资源
      最近更新 更多