JQuery中ajax,get方法在ashx,aspx,asmx中的使用

这篇博客文章给了很大的提示,原地址是http://blog.csdn.net/lovegonghui/article/details/49072363

一、请求ashx的例子:

[html]  view plain  copy
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Request.aspx.cs" Inherits="Ashx_Request" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>请求ashx的例子</title>  
  8.     <script type="text/javascript" src="../Scripts/jquery-1.4.1.min.js"></script>  
  9.     <script type="text/javascript">  
  10.         $(function () {  
  11.   
  12.             //POST请求,数据类型为text  
  13.             /* $.ajax({  
  14.             type: "POST",  
  15.             url: "Response.ashx",  
  16.             contentType: "application/text;charset=utf-8;",  
  17.             dataType: "text",  
  18.             success: function (res) {  
  19.             var re = res;  
  20.             alert(re);  
  21.             },  
  22.             error: function (xmlRes, err, c) {  
  23.             var xml = xmlRes;  
  24.             alert(err);  
  25.             }  
  26.             });*/  
  27.   
  28.             //GET请求,类型为text  
  29.             /*$.get(  
  30.             "Response.ashx",  
  31.             function (res) {  
  32.             alert(res);  
  33.             }  
  34.             )*/  
  35.   
  36.             //GET请求,带参数类型为text  
  37.             /*$.get(  
  38.             "Response.ashx",  
  39.             { "param": "我是请求的传递参数" },  
  40.             function (res) {              
  41.             alert(res);  
  42.             }  
  43.             );*/  
  44.   
  45.             //GET请求,类型为xml  
  46.             /*$.get(  
  47.             "Response.ashx",  
  48.             function (res) {  
  49.             alert($(res).find("gonghui").text());  
  50.             }  
  51.             );*/  
  52.   
  53.             //Ajax的GET请求,类型为xml  
  54.             /*$.ajax({  
  55.             type: "GET",  
  56.             url: "Response.ashx",  
  57.             contentType: "application/xml;charset=utf-8;",  
  58.             dataType: "xml",  
  59.             success: function (res) {  
  60.             var re = $(res).find("gonghui").text();  
  61.             alert(re);  
  62.             },  
  63.             error: function (xmlRes, err, c) {  
  64.             var xml = xmlRes;  
  65.             alert(err);  
  66.             }  
  67.             });*/  
  68.   
  69.             //Ajax的POST请求,类型为json  
  70.             $.ajax({  
  71.                 type: "POST",  
  72.                 url: "Response.ashx",  
  73.                 contentType: "application/json;charset=utf-8;",  
  74.                 dataType: "json",  
  75.                 success: function (res) {  
  76.                     var re = res;  
  77.                     alert(re);  
  78.                 },  
  79.                 error: function (xmlRes, err, c) {   
  80.                     var xml = xmlRes;  
  81.                     alert(err);  
  82.                 }  
  83.             });  
  84.   
  85.         })       
  86.     </script>  
  87. </head>  
  88. <body>  
  89.     <form id="form1" runat="server">  
  90.     <div>  
  91.       
  92.     </div>  
  93.     </form>  
  94. </body>  
  95. </html>  


后台Request.cs

[csharp]  view plain  copy
  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.   
  8. public partial class Ashx_Request : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         Session["sk"] = "我是请求页中请求的session数据";  
  13.     }  
  14. }  


ashx的代码:

[csharp]  view plain  copy
  1. <%@ WebHandler Language="C#" Class="Response" %>  
  2.   
  3. using System;  
  4. using System.Web;  
  5. using System.Web.SessionState;  
  6.   
  7. public class Response : IHttpHandler,IRequiresSessionState {  
  8.       
  9.     public void ProcessRequest (HttpContext context) {  
  10.         //RequestSession(context);  
  11.         //GetRequest(context);  
  12.         //GetRequestParam(context);  
  13.         //GetRequestXml(context);  
  14.         RequestJson(context);  
  15.     }  
  16.    
  17.     public bool IsReusable {  
  18.         get {  
  19.             return false;  
  20.         }  
  21.     }  
  22.   
  23.     private void RequestSession(HttpContext context)  
  24.     {  
  25.         context.Response.ContentType = "text/plain";  
  26.         string sk = context.Session["sk"as string;  
  27.         context.Response.Write(sk);  
  28.     }  
  29.   
  30.     private void GetRequest(HttpContext context)  
  31.     {  
  32.         context.Response.ContentType = "text/plain";  
  33.         context.Response.Write("Hello World");  
  34.     }  
  35.   
  36.     private void GetRequestParam(HttpContext context)  
  37.     {  
  38.         context.Response.ContentType = "text/plain";  
  39.         string param = context.Request["param"] ?? "";  
  40.         context.Response.Write("传递参数值为:" + param);  
  41.     }  
  42.   
  43.   
  44.     private void GetRequestXml(HttpContext context)  
  45.     {  
  46.         context.Response.ContentType = "application/xml";  
  47.         context.Response.Write("<gonghui>Hello World</gonghui>");  
  48.     }  
  49.   
  50.     private void RequestJson(HttpContext context)  
  51.     {  
  52.         context.Response.ContentType = "application/json";  
  53.         context.Response.Write("[\"Hello World.\"]");  
  54.     }  
  55. }  


二、请求webservice方法

Request页面的关键代码:

[javascript]  view plain  copy
  1. $(function () {  
  2.             //Ajax使用POST请求,传递参数  
  3.             /*$.ajax({ 
  4.             type: "POST", 
  5.             url: "WebService.asmx/GetMyName", 
  6.             data: "{'name':'gonghui'}", //请求webservice方法的时候,必须这样传递参数,如果遇到单引号,注意转码。 
  7.             //data:"name=acles", // 请求webservice方法的时候,不能这样传递参数,会报“无效的JSON基元的错误” 
  8.             contentType: "application/json;charset=utf-8", 
  9.             dataType: "json", 
  10.             success: function (res) { 
  11.             alert(res.d); 
  12.             }, 
  13.             error: function (xmlRes, err, c) { 
  14.             alert(err); 
  15.             } 
  16.             });*/  
  17.   
  18.             /*$.ajax({ 
  19.             type: "GET", 
  20.             url: "WebService.asmx/GetXml", 
  21.             contentType: "application/xml;charset=utf-8;", 
  22.             dataType: "xml", 
  23.             success: function (res) { 
  24.             var my = $(res).find("gonghui").text(); 
  25.             alert(my); 
  26.             } 
  27.             });*/  
  28.   
  29.             $.ajax({  
  30.                 type: "POST",  
  31.                 url: "WebService.asmx/GetXmlPost",  
  32.                 contentType: "application/xml;charset=utf-8;",  
  33.                 dataType: "xml",  
  34.                 success: function (res) {  
  35.                     var my = $(res).find("string").text();  
  36.                     alert(my);  
  37.                 }  
  38.             });  
  39.         })  


WebService的关键代码:

[csharp]  view plain  copy
  1. [WebMethod]  
  2.    public string GetMyName(string name) {  
  3.        return "这是WebService页面的值,这是Request传递的参数值:" + name;  
  4.    }  
  5.   
  6.    [WebMethod]  
  7.    [System.Web.Script.Services.ScriptMethod(UseHttpGet=true)]  
  8.    public System.Xml.XmlDocument GetXml() {  
  9.        string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><gonghui>我是通过Get方式请求返回的xml</gonghui>";  
  10.        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();  
  11.        doc.LoadXml(xml);  
  12.        return doc;  
  13.    }  
  14.   
  15.    [WebMethod]  
  16.    public string GetXmlPost() {  
  17.        return "我是通过POST方式请求返回的XML";  
  18.    }  


三、请求Aspx页面的例子

Request页面的关键代码:

[javascript]  view plain  copy
  1. //直接请求页面的方法,其实请求本身页面和其他页面都是一样的,无非就是地址不一样  
  2.        $(function () {  
  3.            //请求自身页面  
  4.            /*$.get( 
  5.            "Request.aspx", 
  6.            { "token": "ajax" }, 
  7.            function (data) { 
  8.            $("#dataShow").text(data); 
  9.            } 
  10.            );*/  
  11.   
  12.            //请求其它页面  
  13.            /*$.ajax({ 
  14.            type: "GET", 
  15.            url: "Response.aspx", 
  16.            data: { token: "ajax" }, 
  17.            dataType: "json", 
  18.            contentType: "application/json;charset=utf-8", 
  19.            success: function (data) { 
  20.            var arr = data; 
  21.            $.each(arr, function (idx, item) { 
  22.            alert(item.StudentID + "/" + item.Hometown); 
  23.            });                     
  24.            }, 
  25.            error: function (d, c, e) { 
  26.            var dd = d; 
  27.            } 
  28.            });*/  
  29.   
  30.            //请求自身页面方法(get方法),返回是自身页面的html源码  
  31.            /*$.get( 
  32.            "Request.aspx/RequestMethod", 
  33.            function (data) { 
  34.            $("#dataShow").text(data); 
  35.            } 
  36.            );*/  
  37.   
  38.            //请求页面,带参数  
  39.            $.ajax({  
  40.                type: "POST",  
  41.                url: "Response.aspx/RequestMethod1",  
  42.                data: "{'msg':'这是一个msg参数值'}",  
  43.                contentType: "application/json;charset=utf-8"//这句话不要忘了  
  44.                dataType: "json",  
  45.                success: function (res) {  
  46.                    $("#dataShow").text(res.d);  
  47.                },  
  48.                error: function (xmlReq, err, c) {  
  49.                    $("#dataShow").text(err);  
  50.                }  
  51.            });  
  52.        })  


Response.cs的关键代码:

[csharp]  view plain  copy
  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         if (!IsPostBack)  
  4.         {  
  5.             StreamReader reader = new StreamReader(Request.InputStream);  
  6.             string dd = reader.ReadToEnd();  
  7.             if ((Request["token"] ?? "") == "ajax")  
  8.             {  
  9.                 //Response.ContentType = "application/Json";  
  10.                 //Response.ContentEncoding = System.Text.Encoding.UTF8;           
  11.                 Response.Clear();  
  12.                 Response.Write("[{\"StudentID\":1,\"Name\":\"tmac\",\"Hometown\":\"GuangDong\"},{\"StudentID\":2,\"Name\":\"gonghui\",\"Hometown\":\"HuNan\"}]");  
  13.                 Response.Flush();  
  14.                 Response.End();  
  15.             }  
  16.             //Response.Write(RequestMethod());  
  17.         }          
  18.     }  
  19.   
  20.     [System.Web.Services.WebMethod]  
  21.     [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]  
  22.     public static string RequestMethod()//方法一定是静态的  
  23.     {  
  24.         return "";  
  25.     }  
  26.   
  27.     [System.Web.Services.WebMethod]  
  28.     public static string RequestMethod1(string msg)  
  29.     {  
  30.         return "这是一个Response页面值,由Request页面的Ajax传入的值:" + msg;  
  31.     }  


本案例源代码的下载地址1:http://download.csdn.net/detail/lovegonghui/9173673

本案例源代码的下载地址2:http://down.51cto.com/data/2104468

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值