最近在忙JavaScript调用webservice问题,网上的例子调用的都是.net发布的webservice,很少有关于调用java发布的webservice
先前找到了采用SOAP方式来调用,可是要拼字符串,拼了好久就是出不来结果。
于是听从论坛上大侠的建议采用jQuery+ajax的方式进行调用,奈何关于jQuery+ajax方式调用的也全都是.net发布的webservice,搞了好久,还是出不来结果
贴上我的html代码:
- HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>JavaScript调用webservice</title> <style type="text/css"> input { width:200px; } </style> <script type="text/javascript" src="jquery.js"> <script type="text/javascript"> //参考网址:http://www.cnblogs.com/Dot-Boy/archive/2008/10/26/1257159.html $(function(){ /* 1、WebService请求类型都为Post,WebService的Url为“[WebServiceUrl]/[WebMethod]” 2、contentType声明为Json 3、data要用Json的字符串格式传入 4、设置了dataType为json后,result就直接为返回的Json对象。 */ //调用无参数方法 $("#btnHelloWorld").click(function(){ $.ajax({ type: "POST", contentType:"application/json", url:"http://localhost:8080/WebServiceShow/services/GISWebService?wsdl/showTime", data:"{}", dataType:'json', success:function(result){ alert(result.d); } }); }); </script></head><body> <h1>采用jQuery+ajax实现</h1> <form action=""> <input type="button" id="btnHelloWorld" value="ShowTime" /> </form></body></html>
贴上我的webservice代码:
- Java code
package com.xiaoair.service;public interface IGISWebService { public String example(String message); public String showTime(); }
- Java code
package com.xiaoair.service;public class GISWebServiceImpl implements IGISWebService { public String example(String message) { return message; } public String showTime() { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String time = sdf.format(new java.util.Date()); return time; }}
------解决方案--------------------