日期:2014-05-18 浏览次数:20889 次
<!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>
package com.xiaoair.service;
public interface IGISWebService {
public String example(String message);
public String showTime();
}
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;
}
}