jQuery Ajax方法調(diào)用 Asp.Net WebService 的詳細(xì)實(shí)例代碼
更新時(shí)間:2011年04月27日 18:50:22 作者:
我在這里將jQuery Ajax 調(diào)用Aspx.Net WebService 的幾個(gè)常用的方法做了一個(gè)整理,提供給正在找這方面內(nèi)容的博友,希望能給學(xué)習(xí)jQuery的朋友一點(diǎn)幫助,可以直接復(fù)制代碼運(yùn)行。
ws.aspx 代碼
<!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 id="Head1" runat="server">
<title></title>
<script src="jquery.js" type="text/javascript"></script>
<style type="text/css">
.hover
{
cursor: pointer; /*小手*/
background: #ffc; /*背景*/
}
.button
{
width: 150px;
float: left;
text-align: center;
margin: 10px;
padding: 10px;
border: 1px solid #888;
}
#dictionary
{
text-align: center;
font-size: 18px;
clear: both;
border-top: 3px solid #888;
}
#loading
{
border: 1px #000 solid;
background-color: #eee;
padding: 20px;
margin: 100px 0 0 200px;
position: absolute;
display: none;
}
#switcher
{
}
</style>
<script type="text/javascript">
//無(wú)參數(shù)調(diào)用
$(document).ready(function() {
$('#btn1').click(function() {
$.ajax({
type: "POST", //訪問WebService使用Post方式請(qǐng)求
contentType: "application/json", //WebService 會(huì)返回Json類型
url: "WebService1.asmx/HelloWorld", //調(diào)用WebService的地址和方法名稱組合 ---- WsURL/方法名
data: "{}", //這里是要傳遞的參數(shù),格式為 data: "{paraName:paraValue}",下面將會(huì)看到
dataType: 'json',
success: function(result) { //回調(diào)函數(shù),result,返回值
$('#dictionary').append(result.d);
}
});
});
});
//有參數(shù)調(diào)用
$(document).ready(function() {
$("#btn2").click(function() {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/GetWish",
data: "{value1:'心想事成',value2:'萬(wàn)事如意',value3:'牛牛牛',value4:2009}",
dataType: 'json',
success: function(result) {
$('#dictionary').append(result.d);
}
});
});
});
//返回集合(引用自網(wǎng)絡(luò),很說(shuō)明問題)
$(document).ready(function() {
$("#btn3").click(function() {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/GetArray",
data: "{i:10}",
dataType: 'json',
success: function(result) {
$(result.d).each(function() {
//alert(this);
$('#dictionary').append(this.toString() + " ");
//alert(result.d.join(" | "));
});
}
});
});
});
//返回復(fù)合類型
$(document).ready(function() {
$('#btn4').click(function() {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/GetClass",
data: "{}",
dataType: 'json',
success: function(result) {
$(result.d).each(function() {
//alert(this);
$('#dictionary').append(this['ID'] + " " + this['Value']);
//alert(result.d.join(" | "));
});
}
});
});
});
==============
var aArray = [“sdf”,”dasd”,”dsa”]; //數(shù)組$.each(aArray,function(iNum,value){ document.write(“序號(hào):”+iNum+” 值:”+value);});var oObj = {one:1,two:2,three:3};$.each(aArray,function(property,value){ document.write(“屬性:”+ property +” 值:”+value);});
==============================================
//返回DataSet(XML)
$(document).ready(function() {
$('#btn5').click(function() {
$.ajax({
type: "POST",
url: "WebService1.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) { //如果沒有上面的捕獲出錯(cuò)會(huì)執(zhí)行這里的回調(diào)函數(shù)
if (status == 'error') {
alert(status);
}
}
});
});
});
//Ajax 為用戶提供反饋,利用ajaxStart和ajaxStop 方法,演示ajax跟蹤相關(guān)事件的回調(diào),他們兩個(gè)方法可以添加給jQuery對(duì)象在Ajax前后回調(diào)
//但對(duì)與Ajax的監(jiān)控,本身是全局性的
$(document).ready(function() {
$('#loading').ajaxStart(function() {
$(this).show();
}).ajaxStop(function() {
$(this).hide();
});
});
// 鼠標(biāo)移入移出效果,多個(gè)元素的時(shí)候,可以使用“,”隔開
$(document).ready(function() {
$('div.button').hover(function() {
$(this).addClass('hover');
}, function() {
$(this).removeClass('hover');
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="switcher">
<h2>
jQuery 的WebServices 調(diào)用</h2>
<div class="button" id="btn1">
HelloWorld</div>
<div class="button" id="btn2">
傳入?yún)?shù)</div>
<div class="button" id="btn3">
返回集合</div>
<div class="button" id="btn4">
返回復(fù)合類型</div>
<div class="button" id="btn5">
返回DataSet(XML)</div>
</div>
<div id="loading">
服務(wù)器處理中,請(qǐng)稍后。
</div>
<div id="dictionary">
</div>
</form>
</body>
</html>
WebService1.asmx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
namespace jQuery.Learning
{
/// <summary>
/// WebService1 的摘要說(shuō)明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
/// <summary>
/// 無(wú)參數(shù)
/// </summary>
/// <returns></returns>
[WebMethod]
public string HelloWorld()
{
return "Hello World ";
}
/// <summary>
/// 帶參數(shù)
/// </summary>
/// <param name="value1"></param>
/// <param name="value2"></param>
/// <param name="value3"></param>
/// <param name="value4"></param>
/// <returns></returns>
[WebMethod]
public string GetWish(string value1, string value2, string value3, int value4)
{
return string.Format("祝您在{3}年里 {0}、{1}、{2}", value1, value2, value3, value4);
}
/// <summary>
/// 返回集合
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
[WebMethod]
public List<int> GetArray(int i)
{
List<int> list = new List<int>();
while (i >= 0)
{
list.Add(i--);
}
return list;
}
/// <summary>
/// 返回一個(gè)復(fù)合類型
/// </summary>
/// <returns></returns>
[WebMethod]
public Class1 GetClass()
{
return new Class1 { ID = "1", Value = "牛年大吉" };
}
/// <summary>
/// 返回XML
/// </summary>
/// <returns></returns>
[WebMethod]
public DataSet GetDataSet()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
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"] = "萬(wàn)事如意";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
return ds;
}
}
//自定義的類,只有兩個(gè)屬性
public class Class1
{
public string ID { get; set; }
public string Value { get; set; }
}
}
復(fù)制代碼 代碼如下:
<!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 id="Head1" runat="server">
<title></title>
<script src="jquery.js" type="text/javascript"></script>
<style type="text/css">
.hover
{
cursor: pointer; /*小手*/
background: #ffc; /*背景*/
}
.button
{
width: 150px;
float: left;
text-align: center;
margin: 10px;
padding: 10px;
border: 1px solid #888;
}
#dictionary
{
text-align: center;
font-size: 18px;
clear: both;
border-top: 3px solid #888;
}
#loading
{
border: 1px #000 solid;
background-color: #eee;
padding: 20px;
margin: 100px 0 0 200px;
position: absolute;
display: none;
}
#switcher
{
}
</style>
<script type="text/javascript">
//無(wú)參數(shù)調(diào)用
$(document).ready(function() {
$('#btn1').click(function() {
$.ajax({
type: "POST", //訪問WebService使用Post方式請(qǐng)求
contentType: "application/json", //WebService 會(huì)返回Json類型
url: "WebService1.asmx/HelloWorld", //調(diào)用WebService的地址和方法名稱組合 ---- WsURL/方法名
data: "{}", //這里是要傳遞的參數(shù),格式為 data: "{paraName:paraValue}",下面將會(huì)看到
dataType: 'json',
success: function(result) { //回調(diào)函數(shù),result,返回值
$('#dictionary').append(result.d);
}
});
});
});
//有參數(shù)調(diào)用
$(document).ready(function() {
$("#btn2").click(function() {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/GetWish",
data: "{value1:'心想事成',value2:'萬(wàn)事如意',value3:'牛牛牛',value4:2009}",
dataType: 'json',
success: function(result) {
$('#dictionary').append(result.d);
}
});
});
});
//返回集合(引用自網(wǎng)絡(luò),很說(shuō)明問題)
$(document).ready(function() {
$("#btn3").click(function() {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/GetArray",
data: "{i:10}",
dataType: 'json',
success: function(result) {
$(result.d).each(function() {
//alert(this);
$('#dictionary').append(this.toString() + " ");
//alert(result.d.join(" | "));
});
}
});
});
});
//返回復(fù)合類型
$(document).ready(function() {
$('#btn4').click(function() {
$.ajax({
type: "POST",
contentType: "application/json",
url: "WebService1.asmx/GetClass",
data: "{}",
dataType: 'json',
success: function(result) {
$(result.d).each(function() {
//alert(this);
$('#dictionary').append(this['ID'] + " " + this['Value']);
//alert(result.d.join(" | "));
});
}
});
});
});
==============
var aArray = [“sdf”,”dasd”,”dsa”]; //數(shù)組$.each(aArray,function(iNum,value){ document.write(“序號(hào):”+iNum+” 值:”+value);});var oObj = {one:1,two:2,three:3};$.each(aArray,function(property,value){ document.write(“屬性:”+ property +” 值:”+value);});
==============================================
//返回DataSet(XML)
$(document).ready(function() {
$('#btn5').click(function() {
$.ajax({
type: "POST",
url: "WebService1.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) { //如果沒有上面的捕獲出錯(cuò)會(huì)執(zhí)行這里的回調(diào)函數(shù)
if (status == 'error') {
alert(status);
}
}
});
});
});
//Ajax 為用戶提供反饋,利用ajaxStart和ajaxStop 方法,演示ajax跟蹤相關(guān)事件的回調(diào),他們兩個(gè)方法可以添加給jQuery對(duì)象在Ajax前后回調(diào)
//但對(duì)與Ajax的監(jiān)控,本身是全局性的
$(document).ready(function() {
$('#loading').ajaxStart(function() {
$(this).show();
}).ajaxStop(function() {
$(this).hide();
});
});
// 鼠標(biāo)移入移出效果,多個(gè)元素的時(shí)候,可以使用“,”隔開
$(document).ready(function() {
$('div.button').hover(function() {
$(this).addClass('hover');
}, function() {
$(this).removeClass('hover');
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="switcher">
<h2>
jQuery 的WebServices 調(diào)用</h2>
<div class="button" id="btn1">
HelloWorld</div>
<div class="button" id="btn2">
傳入?yún)?shù)</div>
<div class="button" id="btn3">
返回集合</div>
<div class="button" id="btn4">
返回復(fù)合類型</div>
<div class="button" id="btn5">
返回DataSet(XML)</div>
</div>
<div id="loading">
服務(wù)器處理中,請(qǐng)稍后。
</div>
<div id="dictionary">
</div>
</form>
</body>
</html>
WebService1.asmx.cs
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
namespace jQuery.Learning
{
/// <summary>
/// WebService1 的摘要說(shuō)明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
/// <summary>
/// 無(wú)參數(shù)
/// </summary>
/// <returns></returns>
[WebMethod]
public string HelloWorld()
{
return "Hello World ";
}
/// <summary>
/// 帶參數(shù)
/// </summary>
/// <param name="value1"></param>
/// <param name="value2"></param>
/// <param name="value3"></param>
/// <param name="value4"></param>
/// <returns></returns>
[WebMethod]
public string GetWish(string value1, string value2, string value3, int value4)
{
return string.Format("祝您在{3}年里 {0}、{1}、{2}", value1, value2, value3, value4);
}
/// <summary>
/// 返回集合
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
[WebMethod]
public List<int> GetArray(int i)
{
List<int> list = new List<int>();
while (i >= 0)
{
list.Add(i--);
}
return list;
}
/// <summary>
/// 返回一個(gè)復(fù)合類型
/// </summary>
/// <returns></returns>
[WebMethod]
public Class1 GetClass()
{
return new Class1 { ID = "1", Value = "牛年大吉" };
}
/// <summary>
/// 返回XML
/// </summary>
/// <returns></returns>
[WebMethod]
public DataSet GetDataSet()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
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"] = "萬(wàn)事如意";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
return ds;
}
}
//自定義的類,只有兩個(gè)屬性
public class Class1
{
public string ID { get; set; }
public string Value { get; set; }
}
}
您可能感興趣的文章:
- Jquery + Ajax調(diào)用webService實(shí)例代碼(asp.net)
- jQuery AJAX 調(diào)用WebService實(shí)現(xiàn)代碼
- 關(guān)于jquery ajax 調(diào)用帶參數(shù)的webservice返回XML數(shù)據(jù)一個(gè)小細(xì)節(jié)
- asp.net下使用jquery 的ajax+WebService+json 實(shí)現(xiàn)無(wú)刷新取后臺(tái)值的實(shí)現(xiàn)代碼
- Jquery ajax傳遞復(fù)雜參數(shù)給WebService的實(shí)現(xiàn)代碼
- Jquery Ajax學(xué)習(xí)實(shí)例6 向WebService發(fā)出請(qǐng)求,返回DataSet(XML) 異步調(diào)用
- Jquery Ajax學(xué)習(xí)實(shí)例3 向WebService發(fā)出請(qǐng)求,調(diào)用方法返回?cái)?shù)據(jù)
- Jquery Ajax學(xué)習(xí)實(shí)例4 向WebService發(fā)出請(qǐng)求,返回實(shí)體對(duì)象的異步調(diào)用
- Jquery Ajax學(xué)習(xí)實(shí)例5 向WebService發(fā)出請(qǐng)求,返回泛型集合數(shù)據(jù)的異步調(diào)用
- jQuery ajax調(diào)用webservice注意事項(xiàng)
相關(guān)文章
jQuery Validate插件自定義驗(yàn)證規(guī)則的方法
這篇文章主要介紹了jQuery Validate插件自定義驗(yàn)證規(guī)則的方法,f非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
Ajax搜索結(jié)果頁(yè)面下方的分頁(yè)按鈕的生成
前幾天在做項(xiàng)目的時(shí)候 遇到個(gè)小問題,就是分頁(yè)結(jié)果的頁(yè)數(shù)太多一般5頁(yè)到10頁(yè)就結(jié)束了可是這個(gè)分頁(yè)結(jié)果有400多頁(yè)的當(dāng)時(shí)就有些抓狂了,后來(lái)想了想模仿搜索引擎的結(jié)果頁(yè)面2012-04-04
jquery中輸入驗(yàn)證中一個(gè)不錯(cuò)的效果
在表單的輸入驗(yàn)證中,經(jīng)常要當(dāng)用戶沒能正確輸入后,要提示“XXXX輸入錯(cuò)誤”這一類的信息,如何能搞到動(dòng)態(tài)一點(diǎn)呢,今天發(fā)現(xiàn)jquery中的一個(gè)不錯(cuò)的效果,筆記之。2010-08-08
jquery插件實(shí)現(xiàn)鼠標(biāo)經(jīng)過(guò)圖片右側(cè)顯示大圖的效果(類似淘寶)
分享一個(gè)jquery插件:實(shí)現(xiàn)類似淘寶上鼠標(biāo)經(jīng)過(guò)圖片右側(cè)顯示大圖的效果,感興趣的朋友可以研究下,或許對(duì)你學(xué)習(xí)jquery有所幫助,千萬(wàn)不要錯(cuò)過(guò)啊2013-02-02
細(xì)說(shuō)瀏覽器特性檢測(cè)(1)-jQuery1.4添加部分
瀏覽器特性檢測(cè)即通過(guò)探測(cè)對(duì)象是否擁有某個(gè)屬性或者函數(shù),或者通過(guò)其他的編碼探測(cè)方式,來(lái)決定其是否支持某一功能、特性。2010-11-11
jQuery實(shí)現(xiàn)兩列等高并自適應(yīng)高度
想要使用jQuery實(shí)現(xiàn)兩列等高并自適應(yīng)高度,其實(shí)也很簡(jiǎn)單,原理就是取得左右兩邊的高度,然后判斷這個(gè)值,把大的值賦給小的就行了。下面就跟小編一起來(lái)看下吧2016-12-12
JQuery實(shí)現(xiàn)表格中相同單元格合并示例代碼
一定要注意如果從list的開始元素循環(huán)下去,remove掉一個(gè)元素后,有些元素就找不到了或者說(shuō)不是要找的那個(gè)元素,感興趣的各位可以研究下哈2013-06-06
Jquery根據(jù)瀏覽器窗口改變調(diào)整大小的方法
下面小編就為大家?guī)?lái)一篇Jquery根據(jù)瀏覽器窗口改變調(diào)整大小的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02

