JQuery实现异步刷新(原创)

     JQuery,是轻量级的js库,把繁琐的js代码封装,使调用更简单,完成更多功能。同样,封装了js利用XMLHttpRequest实现的异步刷新.今天很简单介绍这一封装的调用

  示例如下:

        调用页面:JqueryTest.aspx(注:后台无代码)

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

<!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="JS/jquery-1.4.2.js" type="text/javascript"></script>
    <script language="javascript">
        function test() {
            $.ajax({
                type: "POST",
                url: "JqueryService.aspx",
                data: "userName=123&password=456" ,
                success: function(data) {
                    if (data == "true") {
                        alert("登陆成功!");
                    }
                    else {
                        alert("登陆失败");
                    }
                }
            });
        }
        function btntestajax_onclick() {
            test();
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <input id="btntestajax" type="button" value="测试异步刷新" onclick="return btntestajax_onclick()" /></div>
    </form>
</body>
</html>

被调用界面:JqueryService

前台:

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

后台:

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

namespace XmlHttpRequestTest
{
    public partial class JqueryService : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String userName=Request.Form["userName"].ToString();
            String password=Request.Form["password"].ToString();
            if (userName.Equals("123") && password.Equals("456"))
            {
                Response.Write("true");
            }
            else
            {
                Response.Write("false");
            }
        }
    }
}

注:由于代码过于简单,不做详细说明,有不明白的地方,请自行参考相关资料.

posted @ 2011-03-21 18:25  --中庸--  阅读(4973)  评论(0编辑  收藏  举报