jQuery調(diào)用WebService的實(shí)現(xiàn)代碼
1、.aspx中:
<div class="button" id="btn1"><a href="#">HelloWorld</div>
<div class="button" id="btn2"><a href="#">傳入?yún)?shù)</a></div>
<div class="button" id="btn3"><a href="#">返回集合</a></div>
<div class="button" id="btn4"><a href="#">返回復(fù)合類型</a></div>
<div class="button" id="btn5"><a href="#">返回DataSet(XML)</a></div>
</div><div id="loading">服務(wù)器處理中,請(qǐng)稍后</div>
<div id="dictionary"></div>
2、WebService中:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。
[System.Web.Script.Services.ScriptService] //如果要用jquery調(diào)用WebService則取消前面的注釋
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//如果使用設(shè)計(jì)的組件,請(qǐng)取消注釋以下行
//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;
}
}
//自定義的類,只有兩個(gè)屬性
public class Class1
{
public string ID { get; set; }
public string Value { get; set; }
}
3、JS中:
<script language="javascript" type="text/javascript">
//無參數(shù)
$(function() {
$("#btn1").click(function() {
$.ajax({
type: "POST", //訪問webservice使用POST方式請(qǐng)求
contentType: "application/json;utf-8", //WebService會(huì)返回Json類型
url: "WebService.asmx/HelloWorld", //調(diào)用WebService方法
data: "{}", //要傳遞的參數(shù),沒有傳參時(shí),也一定要寫上
dataType: "json",
success: function(result) {
result =result.d;//返回d后面的json內(nèi)容
$("#dictionary").append(result);
}
});
});
});
//有參數(shù)
$(function() {
$("#btn2").click(function() {
$.ajax({
type: "POST",
contentType: "application/json;utf-8",
url: "WebService.asmx/GetWish",
data: "{value1:'萬事如意',value2:'心想事成',value3:'財(cái)運(yùn)滾滾',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() + " ");
});
}
});
});
});
//返回實(shí)體
$(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) { //如果沒有上面的捕獲出錯(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>
4、效果

- php如何調(diào)用webservice應(yīng)用介紹
- C#發(fā)送HttpPost請(qǐng)求來調(diào)用WebService的方法
- PHP中如何調(diào)用webservice的實(shí)例參考
- 解析利用wsdl.exe生成webservice代理類的詳解
- 深入.net調(diào)用webservice的總結(jié)分析
- c#動(dòng)態(tài)調(diào)用Webservice的兩種方法實(shí)例
- PHP使用SOAP調(diào)用.net的WebService數(shù)據(jù)
- 利用soaplib搭建webservice詳細(xì)步驟和實(shí)例代碼
- WebService教程詳解(一)
相關(guān)文章
2012年開發(fā)人員的16款新鮮的jquery插件體驗(yàn)分享
jQuery的是一個(gè)多瀏覽器的Javascript集合,以輕松地簡(jiǎn)化了客戶端腳本的HTML;使用這些插件方法可以創(chuàng)建高效強(qiáng)大的網(wǎng)頁和Web程序2012-12-12asp.net下使用jquery 的ajax+WebService+json 實(shí)現(xiàn)無刷新取后臺(tái)值的實(shí)現(xiàn)代碼
asp.net下使用jquery 的ajax+WebService+json 實(shí)現(xiàn)無刷新取后臺(tái)值的實(shí)現(xiàn)代碼 ,比頁面刷新更好,用戶體驗(yàn)更好,需要的朋友可以參考下。2010-09-09jquery提示 "object expected"的解決方法
在測(cè)試代碼的時(shí)候,提示object expected,下面的解決方法,可以參考下。2009-12-12jQuery對(duì)象與DOM對(duì)象之間的相互轉(zhuǎn)換
本文主要給大家介紹的是jQuery對(duì)象與DOM對(duì)象之間的相互轉(zhuǎn)換的方法和示例,非常實(shí)用,這里推薦給有需要的小伙伴參考下。2015-03-03ajax與json 獲取數(shù)據(jù)并在前臺(tái)使用簡(jiǎn)單實(shí)例
這篇文章主要介紹了ajax與json 獲取數(shù)據(jù)并在前臺(tái)使用簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01jQuery實(shí)現(xiàn)Select左右復(fù)制移動(dòng)內(nèi)容
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)Select左右復(fù)制移動(dòng)內(nèi)容的實(shí)現(xiàn)方法,代碼非常簡(jiǎn)單,具有參考借鑒價(jià)值,需要的朋友參考下2016-08-08jQuery實(shí)現(xiàn)手機(jī)上輸入后隱藏鍵盤功能
最近做了這樣一個(gè)功能,讓用戶輸入手機(jī)發(fā)送驗(yàn)證碼,輸完11位手機(jī)號(hào)后,自動(dòng)隱藏鍵盤。下面通過本文給大家分享實(shí)現(xiàn)方法,一起看看吧2017-01-01