简单ajax+webservice 例子
一直以为webservice只用来提供外部特殊商务服务的功能模块,所以我一直都是用ashx文件处理后台,今天看了别人的文章恍然明白,原来webservice也可以处理ajax请求,而且一个文件可以同时处理多个请求,和ashx方便多了,不用建立那么文件了。
下面是2个很简单的例子 ,明白道理即可
HTML部分
View Code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
//第一个测试例子
function HelloWorld()
{
$.ajax({
type: "POST",
url: "WebService.asmx/HelloWorld",
data: "name=John",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
}
//两数字相加
function add()
{
$.ajax({
type: "POST",
url: "WebService.asmx/Add",
data: "a="+$("#Text1").val()+"&b="+$("#Text2").val(),
success: function(msg){
$("#Text3").val(msg);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="hello" type="button" value="button" onclick="HelloWorld()" />
<br />
<br />
NumA:<input id="Text1" style="width: 31px" type="text" />
+NumB:<input id="Text2"style="width: 33px" type="text" onblur="add()" />
=<input id="Text3" style="width: 33px" type="text" /></div>
</form>
</body>
</html>
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
//第一个测试例子
function HelloWorld()
{
$.ajax({
type: "POST",
url: "WebService.asmx/HelloWorld",
data: "name=John",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
}
//两数字相加
function add()
{
$.ajax({
type: "POST",
url: "WebService.asmx/Add",
data: "a="+$("#Text1").val()+"&b="+$("#Text2").val(),
success: function(msg){
$("#Text3").val(msg);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="hello" type="button" value="button" onclick="HelloWorld()" />
<br />
<br />
NumA:<input id="Text1" style="width: 31px" type="text" />
+NumB:<input id="Text2"style="width: 33px" type="text" onblur="add()" />
=<input id="Text3" style="width: 33px" type="text" /></div>
</form>
</body>
</html>
webservice部分
View Code
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public void HelloWorld() {
if (Context.Request["name"] != null)
{
Context.Response.Write(Context.Request["name"].ToString());
}
else
{
Context.Response.Write("no");
}
}
[WebMethod]
public string Hello(string name)
{
return string.Format("Hello {0}", name);
}
[WebMethod]
public void Add()
{
if (Context.Request["a"] != null && Context.Request["a"] != null)
{
int anum = int.Parse(Context.Request["a"].ToString());
int bnum = int.Parse(Context.Request["b"].ToString());
int result=anum+bnum;
Context.Response.Write(result.ToString());
}
else
{
Context.Response.Write("no");
}
}
}
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public void HelloWorld() {
if (Context.Request["name"] != null)
{
Context.Response.Write(Context.Request["name"].ToString());
}
else
{
Context.Response.Write("no");
}
}
[WebMethod]
public string Hello(string name)
{
return string.Format("Hello {0}", name);
}
[WebMethod]
public void Add()
{
if (Context.Request["a"] != null && Context.Request["a"] != null)
{
int anum = int.Parse(Context.Request["a"].ToString());
int bnum = int.Parse(Context.Request["b"].ToString());
int result=anum+bnum;
Context.Response.Write(result.ToString());
}
else
{
Context.Response.Write("no");
}
}
}
不要忘记引用jquery.js文件
个人签名:天行健,君子以自强不息