想要一个ajax无刷新的例子或者原理.

解决方案 »

  1.   

    //创建对象
    function create_obj(){
        var http_request = false;    //开始初始化XMLHttpRequest对象
        if(window.XMLHttpRequest) { //Mozilla 浏览器
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {//设置MiME类别
                http_request.overrideMimeType("text/xml");
            }
        }
        else if (window.ActiveXObject) { // IE浏览器
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }    if (!http_request) { // 异常,创建对象实例失败
            return false;
        }    return http_request;
    }//初始化、指定处理函数、发送请求的函数
    //var postStr = "ACT=reply&bookid="+bookid+"&book_content="+ book_content;
    //type:post or get
    function send_request(http_request,url) {
        // 确定发送请求的方式和URL以及是否同步执行下段代码
        http_request.open("POST", url, true);
        http_request.send(null);
    }function InsertFav(uid){
        var http_request = create_obj();
        var obj_Yz=document.getElementById('user_id');
        if(obj_Yz){
            if(obj_Yz.value.length>0){
               http_request.onreadystatechange = function(){
                if (http_request.readyState == 4) {
                    if (http_request.status == 200) {
         var http_result = http_request.responseText;
         alert(http_result);
                     }
                }
            }
            send_request(http_request,"../../Ajax/Fav_Com_Shop.aspx?com_loginName="+uid);
        }
      }
    }
     protected void Page_Load(object sender, EventArgs e)
        {
            string com_loginName = string.Empty;
            if (Request.QueryString["com_loginName"] != null) com_loginName = Request.QueryString["com_loginName"].ToString();
            MyWebService myWebService = new MyWebService();
            if (Session["user_id"]!=null)
            {
                if (!myWebService.isExistFavCompany(com_loginName, Session["user_id"].ToString()))
                {
                    if (myWebService.insertFavourite(com_loginName, 0, 0, 0, 0, 0, Session["user_id"].ToString(),0) > 0)
                    {
                        Response.Write("收藏成功!");
                        Response.End();
                    }
                }
                else
                {
                    Response.Write("该商家已经在你的收藏中了!");
                    Response.End();
                }
            }
            else
            {
                Response.Write("您还没有登陆,请先登陆!");
                Response.End();
            }
        }这是一个ajax无刷新插入数据的例子。
      

  2.   

    http://www.cnblogs.com/singlepine/articles/434373.html
    小山的ajax无刷新树
      

  3.   

    用client-callack无刷新更新服务器Session。Default3.aspxpublic partial class Default3 : System.Web.UI.Page, ICallbackEventHandler //必须实现ICallbackEventHandler
    {
        private string s;
        protected void Page_PreRender(object sender, EventArgs e)
        { 
            string sb = "function changeSession(arg, context) {" + Page.ClientScript.GetCallbackEventReference(this, "arg", "clientCallback", "context", true) + "}";        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "callServerScript", sb, true);
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "callbackScript", "function clientCallback(result) { alert(result); }", true);    }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Session["Test"] = "Original";
        }    #region ICallbackEventHandler 成员    public string GetCallbackResult()
        {
            return s;
        }    public void RaiseCallbackEvent(string eventArgument)
        {
            Session["Test"] = eventArgument;
            s = "Session change to " + eventArgument;
        }    #endregion
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Write(Session["Test"].ToString()); //用于测试
        }
    }<head runat="server">
        <title>无标题页</title>
    <script language="javascript" type="text/javascript">
    // <!CDATA[function Button1_onclick() {
        changeSession(document.getElementById('Text1').value, null);
    }// ]]>
    </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="Text1" type="text" /><input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
            <asp:Button ID="Button2" runat="server" Text="测试Session是否改变" OnClick="Button2_Click" />
        </div>
        </form>
    </body>