Jquery实例中关于Ajax调取数据多集中在页面获取,关于WebService调取介绍很少,这里贴出代码,也是举例性质,后台根据自己情况进行更详细的查询返回。
这里要说明几点:
1、WebService地址必须是全称;
2、数据返回(dataType)为xml,如果要返回Json,代码需要调整;
3、参数写法看实例;
页面部分代码:
<%
...
@ Page Language="C#" AutoEventWireup="true" CodeFile="a.aspx.cs" Inherits="a"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
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
runat
="server"
>
<
title
>
无标题页
</
title
>
<
script
src
="jquery.js"
></
script
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
table
id
="example"
border
="1"
cellpadding
="4"
cellspacing
="0"
>
<
tr
>
<
td
>
标题
</
td
>
<
td
>
内容
</
td
>
</
tr
>
</
table
>
<
p
>
<
input
type
="button"
id
="btn"
value
="显示"
/></
p
>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<
script
language
="javascript"
type
="text/javascript"
>
...
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
$(document).ready(function()...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
$("#btn").click(function()...{
GetWebService("/myAddress/aa.asmx/GetExample",
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{title:"测试",content :"K!又是明天"},
"#example",["Title","Content"]);
});
})
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
function GetWebService(myUrl,dataPrams,tableId,arrFiledId)...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
$.ajax(...{
url:myUrl,
data: dataPrams,
dataType: "xml",
type: "POST",
cache: false,
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
success: function(xml)...{
AddTd(xml,tableId,arrFiledId);
},
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
error:function(xml,status)...{
alert(status);
}
});
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
function AddTd(xml,tableId,arrFiledId)...{
var tb=$(tableId);
//清空表,保留第一行
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
tb.find("tr").each(function(i)...{
if (i>0) $(this).remove();
});
//填充数据
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
$(xml).find("Table1").each(function()...{
var tr=$("<tr></tr>");
for (var j=0;j<arrFiledId.length;j++)
$("<td></td>").html($(this).find(arrFiledId[j]).text()).appendTo(tr);
tr.appendTo(tb);
})
}
</
script
>
</
form
>
</
body
>
</
html
>
asmx代码:
[WebMethod]
public
DataSet GetExample(String title, String content)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("Title", Type.GetType("System.String"));
dt.Columns.Add("Content", Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr["Title"] = "甲方";
dr["Content"] = "好了没?";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Title"] = "乙方";
dr["Content"] = "明天!"+DateTime.Now.ToString("yyyy-M-d H:m:s");
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Title"] = title;
dr["Content"] = content;
dt.Rows.Add(dr);
ds.Tables.Add(dt);
return ds;
}