DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> Jquery + Ajax調用webService實例代碼(asp.net)
Jquery + Ajax調用webService實例代碼(asp.net)
編輯:JQuery特效代碼     
webService中要實現ajax調用,則要加這句代碼:
// 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。
[System.Web.Script.Services.ScriptService]
代碼下載 /201008/yuanma/WebService2.rar
代碼如下:
//無參數調用
$(document).ready(function() {
$('#btn1').click(function() {
$.ajax({
type: "POST", //訪問WebService使用Post方式請求
contentType: "application/json", //WebService 會返回Json類型
url: WebServiceURL + "WebService1.asmx/HelloWorld", //調用WebService的地址和方法名稱組合 ---- WsURL/方法名
data: "{}", //這裡是要傳遞的參數,格式為 data: "{paraName:paraValue}",下面將會看到
dataType: 'json',
success: function(result) { //回調函數,result,返回值
$('#dictionary').append(result.d);
}
});
});
});

//有參數調用
$(document).ready(function() {
$("#btn2").click(function() {
$.ajax({
type: "POST",
contentType: "application/json",
url: WebServiceURL + "WebService1.asmx/GetWish",
data: "{value1:'心想事成',value2:'萬事如意',value3:'牛牛牛',value4:2009}",
dataType: 'json',
success: function(result) {
$('#dictionary').append(result.d);
}
});
});
});

//返回集合(引用自網絡,很說明問題)
$(document).ready(function() {
$("#btn3").click(function() {
$.ajax({
type: "POST",
contentType: "application/json",
url: WebServiceURL + "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(" | "));
});
}
});
});
});

//返回復合類型
$(document).ready(function() {
$('#btn4').click(function() {
$.ajax({
type: "POST",
contentType: "application/json",
url: WebServiceURL + "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(" | "));
});
}
});
});
});
//返回DataSet(XML)
$(document).ready(function() {
$('#btn5').click(function() {
$.ajax({
type: "POST",
url: WebServiceURL + "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) { //如果沒有上面的捕獲出錯會執行這裡的回調函數
if (status == 'error') {
alert(status);
}
}
});
});
});


//Ajax 為用戶提供反饋,利用ajaxStart和ajaxStop 方法,演示ajax跟蹤相關事件的回調,他們兩個方法可以添加給jQuery對象在Ajax前後回調
//但對與Ajax的監控,本身是全局性的
$(document).ready(function() {
$('#loading').ajaxStart(function() {
$(this).show();
}).ajaxStop(function() {
$(this).hide();
});
});
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved