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

一、请求ashx的例子:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Request.aspx.cs" Inherits="Ashx_Request" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>请求ashx的例子</title>
    <script type="text/javascript" src="../Scripts/jquery-1.4.1.min.js"></script>
    <script type="text/javascript">
        $(function () {

            //POST请求,数据类型为text
            /* $.ajax({
            type: "POST",
            url: "Response.ashx",
            contentType: "application/text;charset=utf-8;",
            dataType: "text",
            success: function (res) {
            var re = res;
            alert(re);
            },
            error: function (xmlRes, err, c) {
            var xml = xmlRes;
            alert(err);
            }
            });*/

            //GET请求,类型为text
            /*$.get(
            "Response.ashx",
            function (res) {
            alert(res);
            }
            )*/

            //GET请求,带参数类型为text
            /*$.get(
            "Response.ashx",
            { "param": "我是请求的传递参数" },
            function (res) {            
            alert(res);
            }
            );*/

            //GET请求,类型为xml
            /*$.get(
            "Response.ashx",
            function (res) {
            alert($(res).find("gonghui").text());
            }
            );*/

            //Ajax的GET请求,类型为xml
            /*$.ajax({
            type: "GET",
            url: "Response.ashx",
            contentType: "application/xml;charset=utf-8;",
            dataType: "xml",
            success: function (res) {
            var re = $(res).find("gonghui").text();
            alert(re);
            },
            error: function (xmlRes, err, c) {
            var xml = xmlRes;
            alert(err);
            }
            });*/

            //Ajax的POST请求,类型为json
            $.ajax({
                type: "POST",
                url: "Response.ashx",
                contentType: "application/json;charset=utf-8;",
                dataType: "json",
                success: function (res) {
                    var re = res;
                    alert(re);
                },
                error: function (xmlRes, err, c) { 
                    var xml = xmlRes;
                    alert(err);
                }
            });

        })     
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>


后台Request.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Ashx_Request : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["sk"] = "我是请求页中请求的session数据";
    }
}


ashx的代码:

<%@ WebHandler Language="C#" Class="Response" %>

using System;
using System.Web;
using System.Web.SessionState;

public class Response : IHttpHandler,IRequiresSessionState {
    
    public void ProcessRequest (HttpContext context) {
        //RequestSession(context);
        //GetRequest(context);
        //GetRequestParam(context);
        //GetRequestXml(context);
        RequestJson(context);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    private void RequestSession(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string sk = context.Session["sk"] as string;
        context.Response.Write(sk);
    }

    private void GetRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

    private void GetRequestParam(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string param = context.Request["param"] ?? "";
        context.Response.Write("传递参数值为:" + param);
    }


    private void GetRequestXml(HttpContext context)
    {
        context.Response.ContentType = "application/xml";
        context.Response.Write("<gonghui>Hello World</gonghui>");
    }

    private void RequestJson(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        context.Response.Write("[\"Hello World.\"]");
    }
}


二、请求webservice方法

Request页面的关键代码:

$(function () {
            //Ajax使用POST请求,传递参数
            /*$.ajax({
            type: "POST",
            url: "WebService.asmx/GetMyName",
            data: "{'name':'gonghui'}", //请求webservice方法的时候,必须这样传递参数,如果遇到单引号,注意转码。
            //data:"name=acles", // 请求webservice方法的时候,不能这样传递参数,会报“无效的JSON基元的错误”
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (res) {
            alert(res.d);
            },
            error: function (xmlRes, err, c) {
            alert(err);
            }
            });*/

            /*$.ajax({
            type: "GET",
            url: "WebService.asmx/GetXml",
            contentType: "application/xml;charset=utf-8;",
            dataType: "xml",
            success: function (res) {
            var my = $(res).find("gonghui").text();
            alert(my);
            }
            });*/

            $.ajax({
                type: "POST",
                url: "WebService.asmx/GetXmlPost",
                contentType: "application/xml;charset=utf-8;",
                dataType: "xml",
                success: function (res) {
                    var my = $(res).find("string").text();
                    alert(my);
                }
            });
        })


WebService的关键代码:

 [WebMethod]
    public string GetMyName(string name) {
        return "这是WebService页面的值,这是Request传递的参数值:" + name;
    }

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet=true)]
    public System.Xml.XmlDocument GetXml() {
        string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><gonghui>我是通过Get方式请求返回的xml</gonghui>";
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        doc.LoadXml(xml);
        return doc;
    }

    [WebMethod]
    public string GetXmlPost() {
        return "我是通过POST方式请求返回的XML";
    }


三、请求Aspx页面的例子

Request页面的关键代码:

 //直接请求页面的方法,其实请求本身页面和其他页面都是一样的,无非就是地址不一样
        $(function () {
            //请求自身页面
            /*$.get(
            "Request.aspx",
            { "token": "ajax" },
            function (data) {
            $("#dataShow").text(data);
            }
            );*/

            //请求其它页面
            /*$.ajax({
            type: "GET",
            url: "Response.aspx",
            data: { token: "ajax" },
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            success: function (data) {
            var arr = data;
            $.each(arr, function (idx, item) {
            alert(item.StudentID + "/" + item.Hometown);
            });                    
            },
            error: function (d, c, e) {
            var dd = d;
            }
            });*/

            //请求自身页面方法(get方法),返回是自身页面的html源码
            /*$.get(
            "Request.aspx/RequestMethod",
            function (data) {
            $("#dataShow").text(data);
            }
            );*/

            //请求页面,带参数
            $.ajax({
                type: "POST",
                url: "Response.aspx/RequestMethod1",
                data: "{'msg':'这是一个msg参数值'}",
                contentType: "application/json;charset=utf-8", //这句话不要忘了
                dataType: "json",
                success: function (res) {
                    $("#dataShow").text(res.d);
                },
                error: function (xmlReq, err, c) {
                    $("#dataShow").text(err);
                }
            });
        })


Response.cs的关键代码:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            StreamReader reader = new StreamReader(Request.InputStream);
            string dd = reader.ReadToEnd();
            if ((Request["token"] ?? "") == "ajax")
            {
                //Response.ContentType = "application/Json";
                //Response.ContentEncoding = System.Text.Encoding.UTF8;         
                Response.Clear();
                Response.Write("[{\"StudentID\":1,\"Name\":\"tmac\",\"Hometown\":\"GuangDong\"},{\"StudentID\":2,\"Name\":\"gonghui\",\"Hometown\":\"HuNan\"}]");
                Response.Flush();
                Response.End();
            }
            //Response.Write(RequestMethod());
        }        
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
    public static string RequestMethod()//方法一定是静态的
    {
        return "";
    }

    [System.Web.Services.WebMethod]
    public static string RequestMethod1(string msg)
    {
        return "这是一个Response页面值,由Request页面的Ajax传入的值:" + msg;
    }


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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值