您的位置:首页 > Web前端 > JQuery

如何使用jquery GET方式请求调用asp.net方法

2009-12-04 20:36 686 查看
前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Third.aspx.cs" Inherits="jquery_Learning.Third" %>

<!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>无标题页</title>
<script src="JQUERY.JS" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("#btn1").click(function(){
$.ajax({
type: "GET",
url: "Default.aspx?m=b",
success: function(msg) {
alert(msg);
}
});
});
$("#btn2").click(function(){
$.ajax({
type:"POST",
url:"Default.aspx?m=a",
success:function(msg){
alert(msg);
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="btn1" type="button" value="GET" /><br />
<input id="btn2" type="button" value="POST" />
</div>
</form>
</body>
</html>


 

后台页面代码Default.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace jquery_Learning
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            char method = Convert.ToChar(HttpContext.Current.Request.QueryString["m"]);
            if (method.ToString() == null )
            {
                method = Convert.ToChar(HttpContext.Current.Request.Form["m"]);
            }
            HttpContext.Current.Request.ContentType = "text/plain";
            switch (method)
            {
                case 'a':
                    HttpContext.Current.Response.Write("a");
                    break;
                case 'b':
                    HttpContext.Current.Response.Write("b");
                    break;
            }
            Response.End();
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐