×

jQuery调用WebService的实现代码

作者:Terry2017.02.15来源:Web前端之家浏览:9513评论:0
关键词:JQueryJS
一个例子说尽:
1、.aspx中:
复制代码 代码如下:

<div class="button" id="btn1"><a href="#">HelloWorld</div>
<div class="button" id="btn2"><a href="#">传入参数</a></div>
<div class="button" id="btn3"><a href="#">返回集合</a></div>
<div class="button" id="btn4"><a href="#">返回复合类型</a></div>
<div class="button" id="btn5"><a href="#">返回DataSet(XML)</a></div>
</div><div id="loading">服务器处理中,请稍后</div>
<div id="dictionary"></div>

2、WebService中:
复制代码 代码如下:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService] //如果要用jquery调用WebService则取消前面的注释
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetWish(string value1, string value2, string value3, int value4)
{
return string.Format("祝您在{3}年里 {0}、{1}、{2}", value1, value2, value3, value4);
}
[WebMethod]
public List<int> GetArray(int i)
{
List<int> list = new List<int>();
while (i >= 0)
{
list.Add(i--);
}
return list;
}
[WebMethod]
public Class1 GetClass()
{
Class1 a = new Class1();
a.ID = "1";
a.Value = "牛年大吉";
return a;
}
[WebMethod]
public DataSet GetDataSet()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Table1");
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("Value", Type.GetType("System.String"));
DataRow dr = dt.NewRow();
dr["ID"] = "1";
dr["Value"] = "新年快乐";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = "2";
dr["Value"] = "万事如意";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
return ds;
}
}
//自定义的类,只有两个属性
public class Class1
{
public string ID { get; set; }
public string Value { get; set; }
}

3、JS中:
复制代码 代码如下:

<script language="javascript" type="text/javascript">
//无参数
$(function() {
$("#btn1").click(function() {
$.ajax({
type: "POST", //访问webservice使用POST方式请求
contentType: "application/json;utf-8", //WebService会返回Json类型
url: "WebService.asmx/HelloWorld", //调用WebService方法
data: "{}", //要传递的参数,没有传参时,也一定要写上
dataType: "json",
success: function(result) {
result =result.d;//返回d后面的json内容
$("#dictionary").append(result);
}
});
});
});
//有参数
$(function() {
$("#btn2").click(function() {
$.ajax({
type: "POST",
contentType: "application/json;utf-8",
url: "WebService.asmx/GetWish",
data: "{value1:'万事如意',value2:'心想事成',value3:'财运滚滚',value4:2009}",
dataType: "json",
success: function(result) {
result =result.d;
$("#dictionary").html(result);
}
});
});
});
//返回集合
$(function() {
$("#btn3").click(function() {
$.ajax({
type: "POST",
contentType: "application/json;utf-8",
url: "WebService.asmx/GetArray",
data: "{i:10}",
dataType: "json",
success: function(result) {
result =result.d;
$(result).each(function() {
$("#dictionary").append(this.toString() + " ");
});
}
});
});
});
//返回实体
$(function() {
$("#btn4").click(function() {
$.ajax({
type: "POST",
contentType: "application/json;utf-8",
url: "WebService.asmx/GetClass",
data: "{}",
dataType: 'json',
success: function(result) {
result =result.d;
$("#dictionary").append(result.ID + " " + result.Value);
}
});
});
});
//返回DataSet(XML)
$(document).ready(function() {
$('#btn5').click(function() {
$.ajax({
type: "POST",
url: "WebService.asmx/GetDataSet",
data: "{}",
dataType: 'xml', //返回的类型为XML ,和前面的Json,不一样了
success: function(result) {
//演示一下捕获
try {
$(result).find("Table1").each(function() {
$('#dictionary').append($(this).find("ID").text() + " " + $(this).find("Value").text());
});
}
catch (e) {
alert(e); return;
}
},
error: function(result, status) { //如果没有上面的捕获出错会执行这里的回调函数
if (status == 'error') {
alert(status);
}
}
});
});
});
//Ajax 为用户提供反馈,利用ajaxStart和ajaxStop 方法,演示ajax跟踪相关事件的回调,他们两个方法可以添加给jQuery对象在Ajax前后回调 //但对与Ajax的监控,本身是全局性的
$(document).ready(function() {
$('#loading').ajaxStart(function() {
$(this).show();
}).ajaxStop(function() {
$(this).hide();
});
});
// 鼠标移入移出效果,多个元素的时候,可以使用“,”隔开
$(document).ready(function() {
$('div.button').hover(function() {
$(this).addClass('hover');
}, function() {
$(this).removeClass('hover');
});
});
</script>

4、效果

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/svg1487088000519.html

网友评论文明上网理性发言已有0人参与

发表评论: