成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導(dǎo)航首頁 ? 技術(shù)教程 ? 基于jquery實(shí)現(xiàn)ajax無刷新評論
全站頭部文字 我要出現(xiàn)在這里
基于jquery實(shí)現(xiàn)ajax無刷新評論 722 2024-03-04   

jquery實(shí)現(xiàn)ajax無刷新評論需要用的技術(shù):(本次試驗(yàn)用的是“jquery-1.4.2.js”版本的jquery)

$.post("一般處理程序路徑",{以字典的形式傳遞參數(shù)},function(data,status){``````});
jquery中的基本選擇器操作;

首先創(chuàng)建數(shù)據(jù)庫“T_article”:

主鍵設(shè)置自增;

查看圖片

然后創(chuàng)建一個強(qiáng)類型的DataSet。

接著創(chuàng)建一個“無刷新評論.aspx”頁面:

頁面代碼如下:

  <div>
    <h2>文章:</h2>
    <p>this a text!this a text!this a text!this a text!this a text!this a text!this a text!this a text!
    this a text!this a text!this a text!this a text!this a text!this a text!this a text!this a text!this a text!
    this a text!this a text!this a text!this a text!this a text!this a text!</p>
    <ul id="pinglunlist">
    </ul>
  </div>
  <textarea id="msg" style="margin-left:20px;" cols="40" rows="10"></textarea>
  <input id="btnpinglun" type="button"
    value="評論" />

然后創(chuàng)建兩個一般處理程序WSXPL.ashx(用來插入數(shù)據(jù)的處理程序)和WSXPL1.ashx(用來獲取所有評論數(shù)據(jù)的處理程序);

WSXPL.ashx中的代碼如下:

    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      string msg = context.Request["msg"];
      new T_articleTableAdapter().Insert(context.Request.UserHostAddress, msg, DateTime.Now); //創(chuàng)建一個強(qiáng)類型的實(shí)例,然后調(diào)用Insert()函數(shù)插入;
      context.Response.Write("ok");
    }

WSXPL1.ashx中的代碼如下:

    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      var datas = new T_articleTableAdapter().GetData(); //返回的是一個DataTable
      StringBuilder sb = new StringBuilder(); //創(chuàng)建StringBuilder更加方便的搜集數(shù)據(jù)
      foreach (var data in datas)   //用foreach方法遍歷DataTable
      {//實(shí)現(xiàn)字符串的拼接;每行數(shù)據(jù)用$隔開,每行數(shù)據(jù)的每個元素用|隔開;有利于前臺解析數(shù)據(jù);
        sb.Append(data.ipaddress).Append("|").Append(data.msg).Append("|").Append(data.posttime).Append("$");
      }
      context.Response.Write(sb);
    }

做完這些步驟,操作數(shù)據(jù)庫的部分就已經(jīng)完成了?,F(xiàn)在只要在前臺把一般處理程序返回的數(shù)據(jù)解析一下并附加的相應(yīng)的位置就可以了!

首先在前臺引用“jquery-1.4.2.js”jquery庫;然后開始編寫js腳本;

$(function () {
      $.post("WSXPL1.ashx", function (data, status) { //通過WSXPL1.ashx獲取所有的評論內(nèi)容
        if (status == "success") {
          var result = data.split("$");  //按照$分割字符串
          for (var i = 0; i < result.length - 1; i++) {
            var msg = result[i];
            var line = msg.split("|");   //按照|分割字符串
            var pinglun = $("<li>用戶ID:" + line[0] + "; 評論內(nèi)容:" + line[1] + "; 評論時間:" + line[2] + "</li>");
            $("#pinglunlist").append(pinglun); //把得到的評論結(jié)果追加到ul元素上
          }
        }
        else {
          alert("ajax錯誤!");
        }
      })

      $("#btnpinglun").click(function () {  //設(shè)置btn事件
        var msg = $("#msg").val();
        $.post("ashx/WSXPL.ashx", { "msg": msg }, function (data, status) {
          if (status == "success") {
            if (data == "ok") {
              $.post("WSXPL1.ashx", function (data, status) { //為了實(shí)現(xiàn)評論的時候評論內(nèi)容會自動的添加到ul上
                if (status == "success") {
                  var result = data.split("$");
                  var msg = result[result.length - 2];    //獲取最后一條評論
                  var line = msg.split("|");
                  var pinglun = $("<li>用戶ID:" + line[0] + "; 評論內(nèi)容:" + line[1] + "; 評論時間:" + line[2] + "</li>");
                  $("#pinglunlist").append(pinglun);   //把最后一條評論追加到ul上
                }
                else {
                  alert("ajax錯誤!");
                }
              })
              alert("評論成功!");
            }
            else {
              alert("評論失??!");
            }
          }
        })

      })
    })

做完這些直接運(yùn)行就可以了!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。


主站蜘蛛池模板: 雷霆出击电视剧全集在线观看| 铁血使命演员表全部| 疯狂72小时演员表| 绝对权力全集免费观看| 画皮2电影| 远景山谷1981免费版| 黄婉伶| 冯友薇| 天天操免费视频| 小池里奈| 高钧贤| 台湾电视台| 一元二次方程计算题| 维尼琼斯| 蔡贞安| 电影《三体》| 金鸳鸯| 找保姆| 陈颖芝全部的电影| 思想认识不到位,重视程度不够| 羞羞的影评| 韩国成人网| 《风流艳妇》| 冬日舞蹈教程完整版| 黄湄媚| cctv16体育频道直播| 意大利∴多情少妇| 小红书如何开通店铺| 王家卫作品| 韩诗雅| 四年级上册麻雀的课堂笔记| 马修·麦克费登| 2001年日历全年表| 戴安·梅尔| 荒山之夜| 金沙滩秦腔剧情介绍| 高中历史知识点总结| 宇宙刑事卡邦| 吉泽明步电影| 新三国第95集完整版| 老版《水浒传》|

?。?!站長長期在線接?。?!

網(wǎng)站、小程序:定制開發(fā)/二次開發(fā)/仿制開發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103