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">
//無參數(shù)調(diào)用
$(document).ready(function() {
$('#btn1').click(function() {
$.ajax({
type: "POST", //訪問WebService使用Post方式請求
contentType: "application/json", //WebService 會返回Json類型
url: "WebService1.asmx/HelloWorld", //調(diào)用WebService的地址和方法名稱組合 ---- WsURL/方法名
data: "{}", //這里是要傳遞的參數(shù),格式為 data: "{paraName:paraValue}",下面將會看到
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:'萬事如意',value3:'牛牛牛',value4:2009}",
dataType: 'json',
success: function(result) {
$('#dictionary').append(result.d);
}
});
});
});
//返回集合(引用自網(wǎng)絡(luò),很說明問題)
$(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(“序號:”+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ò)會執(zhí)行這里的回調(diào)函數(shù)
if (status == 'error') {
alert(status);
}
}
});
});
});
//Ajax 為用戶提供反饋,利用ajaxStart和ajaxStop 方法,演示ajax跟蹤相關(guān)事件的回調(diào),他們兩個(gè)方法可以添加給jQuery對象在Ajax前后回調(diào)
//但對與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ù)器處理中,請稍后。
</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 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請取消對下行的注釋。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
/// <summary>
/// 無參數(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"] = "萬事如意";
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">
//無參數(shù)調(diào)用
$(document).ready(function() {
$('#btn1').click(function() {
$.ajax({
type: "POST", //訪問WebService使用Post方式請求
contentType: "application/json", //WebService 會返回Json類型
url: "WebService1.asmx/HelloWorld", //調(diào)用WebService的地址和方法名稱組合 ---- WsURL/方法名
data: "{}", //這里是要傳遞的參數(shù),格式為 data: "{paraName:paraValue}",下面將會看到
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:'萬事如意',value3:'牛牛牛',value4:2009}",
dataType: 'json',
success: function(result) {
$('#dictionary').append(result.d);
}
});
});
});
//返回集合(引用自網(wǎng)絡(luò),很說明問題)
$(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(“序號:”+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ò)會執(zhí)行這里的回調(diào)函數(shù)
if (status == 'error') {
alert(status);
}
}
});
});
});
//Ajax 為用戶提供反饋,利用ajaxStart和ajaxStop 方法,演示ajax跟蹤相關(guān)事件的回調(diào),他們兩個(gè)方法可以添加給jQuery對象在Ajax前后回調(diào)
//但對與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ù)器處理中,請稍后。
</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 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請取消對下行的注釋。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
/// <summary>
/// 無參數(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"] = "萬事如意";
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)無刷新取后臺值的實(shí)現(xiàn)代碼
- Jquery ajax傳遞復(fù)雜參數(shù)給WebService的實(shí)現(xiàn)代碼
- Jquery Ajax學(xué)習(xí)實(shí)例6 向WebService發(fā)出請求,返回DataSet(XML) 異步調(diào)用
- Jquery Ajax學(xué)習(xí)實(shí)例3 向WebService發(fā)出請求,調(diào)用方法返回?cái)?shù)據(jù)
- Jquery Ajax學(xué)習(xí)實(shí)例4 向WebService發(fā)出請求,返回實(shí)體對象的異步調(diào)用
- Jquery Ajax學(xué)習(xí)實(shí)例5 向WebService發(fā)出請求,返回泛型集合數(shù)據(jù)的異步調(diào)用
- jQuery ajax調(diào)用webservice注意事項(xiàng)
相關(guān)文章
Jquery解析json字符串及json數(shù)組的方法
這篇文章主要介紹了Jquery解析json字符串及json數(shù)組的方法,實(shí)例分析了jQuery操作json格式字符串與數(shù)組的相關(guān)技巧,需要的朋友可以參考下2015-05-0512款經(jīng)典的白富美型—jquery圖片輪播插件—前端開發(fā)必備
圖片輪播是網(wǎng)站中的常用功能,用于在有限的網(wǎng)頁空間內(nèi)展示一組產(chǎn)品圖片或者照片,同時(shí)還有非常吸引人的動畫效果,本文向大家推薦12款實(shí)用的 jQuery 圖片輪播效果插件感興趣的朋友可以了解下哦2013-01-01Jquery實(shí)現(xiàn)搜索框提示功能示例代碼
數(shù)據(jù)量很大的情況下使用Ajax去實(shí)現(xiàn)真的不合適,于是想采用Jquery來實(shí)現(xiàn)方法,具體的示例代碼如下,有需求的朋友可以參考下希望對大家有所幫助2013-08-08分享20個(gè)提升網(wǎng)站界面體驗(yàn)的jQuery插件
今天為大家整理20個(gè)提升網(wǎng)站界面的體驗(yàn)的jQuery插件,這些都是比較“新款”的代碼,喜歡的請用到你的網(wǎng)站項(xiàng)目上吧2014-12-12jQuery+CSS實(shí)現(xiàn)的table表格行列轉(zhuǎn)置功能示例
這篇文章主要介紹了jQuery+CSS實(shí)現(xiàn)的table表格行列轉(zhuǎn)置功能,涉及jQuery事件響應(yīng)及頁面元素屬性動態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01jQuery實(shí)現(xiàn)貪吃蛇小游戲(附源碼下載)
網(wǎng)上關(guān)于JS實(shí)現(xiàn)貪吃蛇的文章有很多,最近閑來無事,想著利用jQury來實(shí)現(xiàn)貪吃蛇小游戲,下面實(shí)現(xiàn)這篇文章主要介紹了利用jQuery實(shí)現(xiàn)貪吃蛇小游戲的方法,文中給出的是吸納思路和示例代碼,需要的朋友可以參考下。2017-03-03