jquery-ajax(get)调用c#后台

1)前台Default.aspx

        $(function() {
            var name = "zhcao";
            var password = "870602";
            $.ajax({
                type: "Get",   //访问WebService使用Get方式请求
                url: "Ajax.aspx",
                data: "name=" + name + "&password=" + password+"&action=test",
                success: function(result) {//回调函数,result,返回值
                    alert(result);
                }
            });
        });

2)后台(Ajax.aspx.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace JqueryAjaxDemo
{
    public partial class Ajax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string name = Request.QueryString["name"];
            string password = Request.QueryString["password"];
            string action = Request.QueryString["action"];
            if (action == "test")
            {
                Test();
            }
        }
        public void Test()
        {
            Response.Write("test");
        }
    }
}

建议将Ajax.aspx代码修改成如下,以便获得更加精确的返回信息

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

 

你可能感兴趣的:(jquery-ajax(get)调用c#后台)