我如何通过javascript进行网络服务调用 [英] How can i make a webservice call from javascript

查看:64
本文介绍了我如何通过javascript进行网络服务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用在线教程在netbeans中创建了一个Web服务调用.它的工作原理很不错,只是在加载页面时加载了Web服务.如何更改它,以便在单击链接或图标时调用Web服务?

以下是它生成的代码

Hi,

I created a web service call in netbeans using an online tutorial. It works great except that the web service loads when the page is loaded. how can i change it so that the webservice gets called when a link or icon is clicked?

below is the code that it generated

<%
     try {
         webservice.CalculatorService service = new webservice.CalculatorService();
         webservice.Calculator port = service.getCalculatorPort();
         int nodeType = 0;
         java.lang.String result = port.treeList(nodeType);
     } catch (Exception ex) {
         //out.println("Error " + ex);
     }
%>



我可以将其设置为可通过jsp页面上的链接或按钮单击来调用的函数吗?



can i make this into a function where it can be called from a link or button click on the jsp page?

推荐答案

Hi.
我是一个老派"开发人员,因此您可能会把这个回应当作一个笑话.只需更改以请求参数为导向的页面流,您就可以在一个流中编写客户端HTML代码,而在另一流中,可以通过该调用中的请求参数来调用WS.例如,假设这是您的JSP页面(test.jsp):

Hi.
I''m an "old school" developer, so you may take this response as a joke. Simply by changing the flow of the page guided by a request parameter, you can write client HTML code in one flow, and in other flow call the WS by the request parameter argumented in that call. For example, suppose this is your JSP page (test.jsp) :

...
<![CDATA[<%
         if (request.getParameter("yesWeCan") != null){
             try {             
               webservice.CalculatorService service = new webservice.CalculatorService();
               webservice.Calculator port = service.getCalculatorPort();
               int nodeType = 0;
               java.lang.String result = port.treeList(nodeType);             
             } catch (Exception ex) {
                 //out.println("Error " + ex);
             }
         }
	%>

...



然后您就可以从Javascript调用JSP页面,如下所示:
http://.../test.jsp?yesWeCan = true

此外,当参数"yesWeCan"不为null时,您不需要向客户端编写任何HTML代码,因此您可以使用Ajax以异步方式调用此代码.例如,

希望这会有所帮助.

问候.



and from your Javascript you''ve goto to call JSP page like this :
http://.../test.jsp?yesWeCan=true

Furthermore, when parameter "yesWeCan" is not null, you don''t need to write any HTML code to the client, so you can call this in an asynchronous way using Ajax, for example.

Hope this helps.

Regards.


解决方案很简单,您需要的只是jquery
solution is simple all you need is jquery


.ajax函数,为此我给了一个小应用程序在我的项目中使用过.

您需要配置的只是提供您网页的url到它的web方法,并提供它给出的数据类型,就我而言,我写的json是dataType

所有设置都可以通过result.d
获取结果
url1 ="yourpage.aspx/webmethod"
.ajax function and to do so i''d given a small app which i''d used in my project.

all you need to configure is provide url of you page upto it''s webmethod and provide datatype it''s giving as in my case i''d written json is dataType

and all set you can fetch result by result.d

url1 = "yourpage.aspx/webmethod"


这篇关于我如何通过javascript进行网络服务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆