jQuery AJaxµ÷ÓÃasp.net webserversµÄʵÏÖ´úÂë

¡¡¡¡aspxÒ³Ãæ´úÂë

¡¡¡¡

¸´ÖÆ´úÂë ´úÂëÈçÏÂ:

¡¡¡¡<html xmlns="http://www.w3.org/1999/xhtml" >

¡¡¡¡<head 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;

¡¡¡¡}

¡¡¡¡

¡¡¡¡--></style><style type="text/css" bogus="1"><!--

¡¡¡¡.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;

¡¡¡¡}

¡¡¡¡

¡¡¡¡--></style><style type="text/css" bogus="1" bogus="1">.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;

¡¡¡¡}

¡¡¡¡</style>

¡¡¡¡<script type="text/javascript"><!--

¡¡¡¡¡¡¡¡//ÎÞ²ÎÊýµ÷ÓÃ

¡¡¡¡¡¡¡¡$(document).ready(function() {

¡¡¡¡¡¡¡¡¡¡¡¡$('#btn1').click(function() {

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡$.ajax({

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡type: "POST",¡¡ //·ÃÎÊWebServiceʹÓÃPost·½Ê½ÇëÇó

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡contentType: "application/json", //WebService »á·µ»ØJsonÀàÐÍ

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡url: "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: "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: "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: "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: "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();

¡¡¡¡¡¡¡¡¡¡¡¡});

¡¡¡¡¡¡¡¡});

¡¡¡¡¡¡¡¡// Êó±êÒÆÈëÒÆ³öЧ¹û£¬¶à¸öÔªËØµÄʱºò£¬¿ÉÒÔʹÓá°£¬¡±¸ô¿ª

¡¡¡¡¡¡¡¡$(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 µ÷ÓÃ</h2>

¡¡¡¡¡¡¡¡<div class="button" id="btn1">

¡¡¡¡¡¡¡¡¡¡¡¡HelloWorld</div>

¡¡¡¡¡¡¡¡<div class="button" id="btn2">

¡¡¡¡¡¡¡¡¡¡¡¡´«Èë²ÎÊý</div>

¡¡¡¡¡¡¡¡<div class="button" id="btn3">

¡¡¡¡¡¡¡¡¡¡¡¡·µ»Ø¼¯ºÏ</div>

¡¡¡¡¡¡¡¡<div class="button" id="btn4">

¡¡¡¡¡¡¡¡¡¡¡¡·µ»Ø¸´ºÏÀàÐÍ</div>

¡¡¡¡¡¡¡¡<div class="button" id="btn5">

¡¡¡¡¡¡¡¡¡¡¡¡·µ»ØDataSet(XML)</div>

¡¡¡¡</div>

¡¡¡¡<div id="loading">

¡¡¡¡¡¡¡¡·þÎñÆ÷´¦ÀíÖУ¬ÇëÉÔºó¡£

¡¡¡¡</div>

¡¡¡¡<div id="dictionary">

¡¡¡¡</div>

¡¡¡¡</form>

¡¡¡¡</body>

¡¡¡¡</html>

¡¡¡¡WebService1.asmx ´úÂë

¡¡¡¡

¸´ÖÆ´úÂë ´úÂëÈçÏÂ:

¡¡¡¡using System;

¡¡¡¡using System.Collections;

¡¡¡¡using System.ComponentModel;

¡¡¡¡using System.Data;

¡¡¡¡using System.Linq;

¡¡¡¡using System.Web;

¡¡¡¡using System.Web.Services;

¡¡¡¡using System.Web.Services.Protocols;

¡¡¡¡using System.Xml.Linq;

¡¡¡¡using System.Collections.Generic;

¡¡¡¡namespace jquery_Learning

¡¡¡¡{

¡¡¡¡/// <summary>

¡¡¡¡/// WebService1 µÄժҪ˵Ã÷

¡¡¡¡/// </summary>

¡¡¡¡[WebService(Namespace = "http://tempuri.org/")]

¡¡¡¡[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

¡¡¡¡[System.ComponentModel.ToolboxItem(false)]

¡¡¡¡// ÈôÒªÔÊÐíʹÓà ASP.NET AJAX ´Ó½Å±¾Öе÷ÓÃ´Ë Web ·þÎñ£¬ÇëÈ¡Ïû¶ÔÏÂÐеÄ×¢ÊÍ¡£

¡¡¡¡[System.Web.Script.Services.ScriptService]

¡¡¡¡public class WebService1 : System.Web.Services.WebService

¡¡¡¡{

¡¡¡¡/// <summary>

¡¡¡¡/// ÎÞ²ÎÊý

¡¡¡¡/// </summary>

¡¡¡¡/// <returns></returns>

¡¡¡¡[WebMethod]

¡¡¡¡public string HelloWorld()

¡¡¡¡{

¡¡¡¡return "Hello World ";

¡¡¡¡}

¡¡¡¡/// <summary>

¡¡¡¡/// ´ø²ÎÊý

¡¡¡¡/// </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>

¡¡¡¡/// ·µ»ØÒ»¸ö¸´ºÏÀàÐÍ

¡¡¡¡/// </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;

¡¡¡¡}

¡¡¡¡}

¡¡¡¡//×Ô¶¨ÒåµÄÀֻ࣬ÓÐÁ½¸öÊôÐÔ

¡¡¡¡public class Class1

¡¡¡¡{

¡¡¡¡public string ID { get; set; }

¡¡¡¡public string Value { get; set; }

¡¡¡¡}

¡¡¡¡}