1)通过webservice,注意去掉注释[System.Web.Script.Services.ScriptService]这行前的注释
2)通过aspx.cs文件中的静态方法
3)通过aspx文件url
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="asp.net.WebForm1" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 <html xmlns="http://www.w3.org/1999/xhtml"> 5 <head runat="server"> 6 <title></title> 7 <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> 8 <script type="text/javascript"> 9 function Ws() { 10 $.ajax({ 11 type: "POST", 12 contentType: "application/json; charset=utf-8", 13 url: "WebService1.asmx/HelloWorld2", 14 data: "{name:‘xiaoxiao‘}", 15 dataType: ‘json‘, 16 success: function (result) { 17 alert(result.d); 18 } 19 }); 20 } 21 function StaticMethod() { 22 $.ajax({ 23 type: "POST", 24 contentType: "application/json; charset=utf-8", 25 url: "aspxpage.aspx/SayHello2", 26 data: "{name:‘xiaoxiao‘}", 27 dataType: ‘json‘, 28 success: function (result) { 29 alert(result.d); 30 } 31 }); 32 33 } 34 function FromPage() { 35 $.ajax({ 36 type: "POST", 37 contentType: "application/json; charset=utf-8", 38 url: "dataContent.aspx?nowtime=‘" + new Date() + "‘", 39 data: "{}", 40 dataType: ‘html‘, 41 success: function (result) { 42 alert(result); 43 } 44 }); 45 46 } 47 48 </script> 49 </head> 50 <body> 51 <form id="form1" runat="server"> 52 53 <div> 54 <input id="Button1" type="button" value="jquery调用WebService" onclick="Ws()" /> 55 </div> 56 <div> 57 <input id="Button2" type="button" value="jquery调用aspx页面静态方法" onclick="StaticMethod()" /> 58 </div> 59 <div> 60 <input id="Button3" type="button" value="jquery通过page存储值" onclick="FromPage()" /> 61 </div> 62 </form> 63 </body> 64 </html>
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Services; 6 7 namespace asp.net 8 { 9 /// <summary> 10 /// WebService1 的摘要说明 11 /// </summary> 12 [WebService(Namespace = "http://tempuri.org/")] 13 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 14 [System.ComponentModel.ToolboxItem(false)] 15 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 16 [System.Web.Script.Services.ScriptService] 17 public class WebService1 : System.Web.Services.WebService 18 { 19 20 [WebMethod] 21 public string HelloWorld() 22 { 23 return "Hello World"+System.DateTime.Now.ToLongTimeString(); 24 } 25 26 [WebMethod] 27 public string HelloWorld2(string name) 28 { 29 return "Hello World" + name + System.DateTime.Now.ToLongTimeString(); 30 } 31 } 32 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Web.Services; 8 9 namespace asp.net 10 { 11 public partial class aspx页面代替ws : System.Web.UI.Page 12 { 13 protected void Page_Load(object sender, EventArgs e) 14 { 15 16 } 17 [WebMethod] 18 public static string SayHello() 19 { 20 return "Hello"; 21 } 22 23 [WebMethod] 24 public static string SayHello2(string name) 25 { 26 return "Hello"+name; 27 } 28 } 29 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.IO; 8 9 namespace asp.net 10 { 11 public partial class dataContent : System.Web.UI.Page 12 { 13 protected void Page_Load(object sender, EventArgs e) 14 { 15 Response.Clear(); 16 Page.ViewStateMode = ViewStateMode.Disabled; 17 if (Request.QueryString["nowtime"] != null) 18 { 19 string stime = Request.QueryString["nowtime"].ToString(); 20 Response.Write(stime); 21 } 22 Response.Flush(); 23 24 } 25 } 26 }