美高梅官方网站3045-mgm6608美高梅app下载
ajax+asp.net+mssql无刷新聊天室

ajax+asp.net+mssql无刷新聊天室

作者:美高梅官方网站    来源:未知    发布时间:2020-01-27 07:52    浏览量:

图片 1

图片 2

最近自己做了一个小demo,实现了Ajax与MySQL的数据交互,js部分用的是jq,后台用的是php,数据库是mysql,过时间再来一个node+mongodb版的。

此为asp版本的无刷新聊天程序源代码,支持htmlweb编辑器。下面只列出asp版本的代码,js类库及说明请看下面asp.net版本的,php版本也在下面。asp.net版本ajax+asp.net+mssql无刷新聊天室php版本ajax+php+mssql无刷新聊天室不过有个小问题,就是firefox下如果使用快捷键ctrl+enter发送信息时,如果不延时调用发送函数,会弹出ff的下载对话框,奇怪。谁能解决这个问题,100分归他,其他100分当作散分。

以前说要放出来的,由于忙其他的事,没整理,现在放出来供大家参考下。不过有个小问题,就是firefox下如果使用快捷键ctrl+enter发送信息时,如果不延时调用发送函数,会弹出ff的下载对话框,奇怪。谁能解决这个问题,100分归他,其他100分当作散分。

关于mysql的使用和安装,就不多讲了,自行百度xampp,Apache服务器和mysql数据库集成,非常好用。

if(e.ctrlKeye.keyCode==13){e.preventDefault();e.stopPropagation();setTimeout("Showbo.Chat.send()",50);//在ff中马上使用alert提示时尽然弹出下载工具,奇怪.returnfalse;}},false);
if(e.ctrlKeye.keyCode==13){e.preventDefault();e.stopPropagation();setTimeout("Showbo.Chat.send()",50);//在ff中马上使用alert提示时尽然弹出下载工具,奇怪.returnfalse;}},false);

首先打开服务器和数据库,我这里先建立了一个“eleven”的数据库,下面建立了一个叫做microblog的表(请注意:我这里使用的是高版本的mysql,里面php链接数据库的方法使用的都是mysqli_  如果版本过低,请使用mysql_ 方法,自行修改代码)
以下是代码部分:

为了加快速度,使用了存储过程。要使用不同的动态页面,只需要更改lib.js文件中RequstUrl变量的值即可。完整示例下载效果如下内容长度限制,所以就不贴出所有的代码了,要看全部代码请下载示例或者查看下面这篇文章ajax+asp.net+mssql无刷新聊天室op.asp

ajax+asp.net+mssql实现的ajax无刷新聊天室,支持htmlweb编辑器。为了加快速度,使用了存储过程。aspphp版本请看下面的文章ajax+asp+mssql无刷新聊天室ajax+php+mssql无刷新聊天室此例子也可以作为mssql存储过程使用的示例,如php调用mssql的存储过程,php获取mssql数据库的输出参数。要使用不同的动态页面,只需要更改lib.js文件中RequstUrl变量的值即可。完整示例下载效果如下内容长度限制,所以就不贴出所有的代码了,要看全部代码请下载示例或者查看下面这篇文章ajax+asp.net+mssql无刷新聊天室下面贴出C#的主要的代码,其他版本请查看上面的链接ajax.cs

html页面和js部分:

%@language="vbscript"codepage="936"%%ConstadCmdStoredProc=4ConstadInteger=3ConstadVarChar=200ConstadParamInput=1ConstadParamOutput=2ConstadExecuteNoRecords=128Response.Charset="gb2312"Response.ContentType="text/html"Dimjson,op,cnfunctionisNL(v)isNL=trim(v"")=""endfunctionfunctionjs(v)js=replace(v,"'","'")endFunctionFunctionopendbSetcn=server.CreateObject("ADODB.Connection")cn.open"driver={sqlserver};server=.;database=chatdb;uid=sa;pwd=;"'注意修改驱动EndFunctionFunctionclosedbcn.close:Setcn=NothingEndfunctionfunctionlogin()'=============登陆系统DimrStr,UserName:rStr="":UserName=Request.Form("nn")IfisNL(UserName)ThenrStr="success:false,err:'昵称不能为空!'"ElseIfLen(UserName)20ThenrStr="success:false,err:'昵称不能超过20个字符!'"ElseDimUserId,Key,cm:UserId="":Key=""CallopendbSetcm=Server.CreateObject("ADODB.Command")cm.CommandType=adCmdStoredProc:cm.CommandText="ajaxLogin":cm.ActiveConnection=cncm.Parameters.Appendcm.CreateParameter("@UserName",adVarChar,adParamInput,50,UserName)cm.Parameters.Appendcm.CreateParameter("@userid",adVarChar,adParamOutput,18)cm.Parameters.Appendcm.CreateParameter("@UserKey",adVarChar,adParamOutput,5)cm.Execute,,adExecuteNoRecordsUserId=cm.Parameters("@userid"):Key=cm.Parameters("@userkey")IfUserId="-1"ThenrStr="success:false,err:'发生错误,请稍后再试!'"ElseIfUserId="0"ThenrStr="success:false,err:'已经存在此用户昵称,请修改您的昵称!'"ElserStr="success:true,UserId:'"UserId"',Key:'"Key"'"EndIfCallclosedbEndIflogin=rStrendfunctionfunctionlogout()'==========退出系统DimrStr,UserId,Key:UserId=Request.Form("uid"):Key=Request.Form("key")IfisNL(UserId)OrisNL(Key)Thenlogout="success:false,err:'用户信息丢失!'"ExitFunctionEndIfCallopendbDimcmSetcm=Server.CreateObject("ADODB.Command")cm.CommandText="ajaxLogout"cm.ActiveConnection=cncm.CommandType=adCmdStoredProccm.Parameters.Appendcm.CreateParameter("@userid",adVarChar,adParamInput,18,UserId)cm.Parameters.Appendcm.CreateParameter("@userkey",adVarChar,adParamInput,5,Key)cm.Parameters.Appendcm.CreateParameter("@result",adInteger,adParamOutput)cm.Execute,,adExecuteNoRecordsIfcm.Parameters("@result")="0"ThenrStr="success:false,err:'用户信息不存在!'"ElserStr="success:true"EndIfCallclosedblogout=rStrendfunctionfunctionreaduser()'==========读取用户列表信息DimrStr,UserId:UserId=Request.Form("uid"):rStr=""IfisNL(UserId)Thenreaduser="success:false,err:'用户Id信息丢失!'"ExitFunctionEndIfUserId=Replace(UserId,"'","")'sql注入替换DimrsCallopendbSetrs=cn.Execute("execajaxReadUser'"UserId"'")WhileNotrs.EOFrStr=rStr",{id:'"rs(0)"',nn:'"js(rs(1))"'}"rs.MoveNextWendrs.Close:Setrs=NothingCallclosedbIfrStr""ThenrStr="success:true,data:["Right(rStr,len(rStr)-1)"]"ElserStr="success:true,data:[]"EndIfreaduser=rStrendfunctionFunctionread()'==========读取信息列表DimrStr,UserId,Key:UserId=Request.Form("uid"):Key=Request.Form("key"):rStr=""IfisNL(UserId)OrisNL(key)Thenread="success:false,err:'用户信息丢失!'"ExitFunctionEndIfUserId=Replace(UserId,"'","")'sql注入替换key=Replace(key,"'","")'sql注入替换DimrsCallopendbSetrs=cn.Execute("execajaxRead'"UserId"','"key"'")WhileNotrs.EOFrStr=rStr",'"js(rs(0))"'"'注意js替换rs.MoveNextWendrs.Close:Setrs=NothingCallclosedbIfrStr""ThenrStr="success:true,data:["Right(rStr,len(rStr)-1)"]"ElserStr="success:true,data:[]"EndIfread=rStrendfunctionfunctionsay()'==========发言DimrStr,From,Key,ToUser,msg,cmFrom=Request.Form("from"):Key=Request.Form("key"):ToUser=Request.Form("to"):msg=Request.Form("ct"):rStr=""IfisNL(from)OrisNL(key)OrisNL(ToUser)OrisNL(msg)Thensay="success:false,err:'信息传递不完整!'"ExitFunctionEndIfSetcm=Server.CreateObject("ADODB.Command")callopendbcm.ActiveConnection=cncm.CommandText="ajaxsay"cm.CommandType=adCmdStoredProccm.Parameters.Appendcm.CreateParameter("@userkey",adVarChar,adParamInput,5,Key)cm.Parameters.Appendcm.CreateParameter("@from",adVarChar,adParamInput,18,from)cm.Parameters.Appendcm.CreateParameter("@to",adVarChar,adParamInput,18,ToUser)cm.Parameters.Appendcm.CreateParameter("@msg",adVarChar,adParamInput,800,msg)cm.Parameters.Appendcm.CreateParameter("@result",adInteger,adParamOutput)cm.Execute,,adExecuteNoRecordsIfcm.Parameters("@result")="0"ThenrStr="success:false,err:'发表失败!n原因:接收者已经不存在!'"ElserStr="success:true"EndIfCallclosedbsay=rStrendFunctionjson="{":op=Request.Form("op")ifop="login"thenJson=jsonLogin()elseifop="logout"thenJson=jsonLogout()elseifop="readuser"thenJson=JsonReadUser()elseifop="read"thenJson=jsonRead()elseifop="say"thenJson=jsonSay()elseJson=Json"success:false,err:'参数错误!'"endifJson=json"}"Response.WriteJson%
usingSystem;usingSystem.Web;usingSystem.Data;usingSystem.Configuration;usingSystem.Data.SqlClient;publicclassAjax{privatestaticboolIsNull(stringv){if(v==null||v.Trim()=="")returntrue;elsereturnfalse;}privatestaticstringJs(stringv){returnv.Replace("'","\'");}publicstaticstringLogin(){HttpRequestRequest=HttpContext.Current.Request;stringrStr="";stringUserName=Request.Form["nn"];if(IsNull(UserName)){rStr="success:false,err:'昵称不能为空!'";}elseif(UserName.Length20){rStr="success:false,err:'昵称不能超过20个字符!'";}else{stringUserId="",Key="";SqlConnectioncn=newSqlConnection(ConfigurationSettings.AppSettings["db"]);cn.Open();try{SqlCommandcm=newSqlCommand("ajaxLogin",cn);cm.CommandType=CommandType.StoredProcedure;cm.Parameters.Add(newSqlParameter("@UserName",SqlDbType.NVarChar,50));cm.Parameters["@UserName"].Value=UserName;//==========输出参数cm.Parameters.Add(newSqlParameter("@UserId",SqlDbType.NVarChar,18));cm.Parameters["@UserId"].Direction=ParameterDirection.Output;cm.Parameters.Add(newSqlParameter("@UserKey",SqlDbType.NVarChar,5));cm.Parameters["@UserKey"].Direction=ParameterDirection.Output;cm.ExecuteNonQuery();UserId=cm.Parameters["@UserId"].Value.ToString().Trim();Key=cm.Parameters["@UserKey"].Value.ToString().Trim();if(UserId=="-1")rStr=@"success:false,err:'发生错误,请稍后再试!'";elseif(UserId=="0")rStr=@"success:false,err:'已经存在此用户昵称,请修改您的昵称!'";elserStr+="success:true,UserId:'"+UserId+"',Key:'"+Key+"'";cm.Dispose();}catch(Exceptione){rStr=@"success:false,err:'原因\n"+Js(e.Message)+"'";}cn.Close();}returnrStr;}publicstaticstringLogout(){HttpRequestRequest=HttpContext.Current.Request;stringrStr="",UserId=Request.Form["uid"],Key=Request.Form["key"];if(IsNull(UserId)||IsNull(Key))return"success:false,err:'用户信息丢失!'";SqlConnectioncn=newSqlConnection(ConfigurationSettings.AppSettings["db"]);cn.Open();try{SqlCommandcm=newSqlCommand("ajaxLogout",cn);cm.CommandType=CommandType.StoredProcedure;cm.Parameters.Add(newSqlParameter("@UserId",SqlDbType.NVarChar,18));cm.Parameters["@UserId"].Value=UserId;cm.Parameters.Add(newSqlParameter("@UserKey",SqlDbType.NVarChar,5));cm.Parameters["@UserKey"].Value=Key;cm.Parameters.Add(newSqlParameter("@Result",SqlDbType.Int));cm.Parameters["@Result"].Direction=ParameterDirection.Output;cm.ExecuteNonQuery();if(cm.Parameters["@UserId"].Value.ToString().Trim()=="0")rStr="success:false,err:'用户信息不存在!'";elserStr="success:true";cm.Dispose();}catch(Exceptione){}cn.Close();returnrStr;}publicstaticstringSay(){HttpRequestRequest=HttpContext.Current.Request;stringFrom=Request.Form["from"],To=Request.Form["to"],Key=Request.Form["key"],Msg=Request.Form["ct"],rStr="";if(IsNull(From)||IsNull(Key)||IsNull(To)||IsNull(Msg))rStr="success:false,err:'信息传递不完整!'";else{SqlConnectioncn=newSqlConnection(ConfigurationSettings.AppSettings["db"]);cn.Open();try{SqlCommandcm=newSqlCommand("ajaxSay",cn);cm.CommandType=CommandType.StoredProcedure;cm.Parameters.Add(newSqlParameter("@UserKey",SqlDbType.NVarChar,5));cm.Parameters["@UserKey"].Value=Key;cm.Parameters.Add(newSqlParameter("@From",SqlDbType.NVarChar,18));cm.Parameters["@From"].Value=From;cm.Parameters.Add(newSqlParameter("@To",SqlDbType.NVarChar,18));cm.Parameters["@To"].Value=To;cm.Parameters.Add(newSqlParameter("@Msg",SqlDbType.NVarChar,800));cm.Parameters["@Msg"].Value=Msg;cm.Parameters.Add(newSqlParameter("@Result",SqlDbType.Int));cm.Parameters["@Result"].Direction=ParameterDirection.Output;cm.ExecuteNonQuery();if(cm.Parameters["@Result"].Value.ToString()=="0")rStr="sucess:false,err:'发表失败!\n原因:接收者已经不存在!'";elserStr="success:true";cm.Dispose();}catch(Exceptione){rStr="sucess:false,err:'发表失败!原因\n"+Js(e.Message)+"'";}cn.Close();}returnrStr;}publicstaticstringReadUser(){HttpRequestRequest=HttpContext.Current.Request;stringrStr="",UserId=Request.Form["uid"];if(IsNull(UserId))rStr+="success:false,err:'用户id丢失!'";else{SqlConnectioncn=newSqlConnection(ConfigurationSettings.AppSettings["db"]);cn.Open();try{SqlCommandcm=newSqlCommand("ajaxReadUser",cn);cm.CommandType=CommandType.StoredProcedure;cm.Parameters.Add(newSqlParameter("@UserId",SqlDbType.NVarChar,18));cm.Parameters["@UserId"].Value=UserId;stringj="";SqlDataReaderdr=cm.ExecuteReader();while(dr.Read())j+=",{id:'"+dr[0]+"',nn:'"+Js(dr[1].ToString())+"'}";dr.Close();cm.Dispose();rStr="success:true,data:["+(j==""?"":j.Substring(1))+"]";}catch(Exceptione){rStr=@"success:false,err:'发生如下错误\n"+Js(e.Message)+"'";}cn.Close();}returnrStr;}publicstaticstringRead(){HttpRequestRequest=HttpContext.Current.Request;stringrStr="";stringUserId=Request.Form["uid"],Key=Request.Form["key"];if(IsNull(UserId)||IsNull(Key))rStr="success:false,err:'用户信息丢失!'";else{SqlConnectioncn=newSqlConnection(ConfigurationSettings.AppSettings["db"]);cn.Open();try{SqlCommandcm=newSqlCommand("ajaxRead",cn);cm.CommandType=CommandType.StoredProcedure;cm.Parameters.Add(newSqlParameter("@UserId",SqlDbType.NVarChar,18));cm.Parameters["@UserId"].Value=UserId;cm.Parameters.Add(newSqlParameter("@UserKey",SqlDbType.NVarChar,5));cm.Parameters["@UserKey"].Value=Key;SqlDataReaderdr=cm.ExecuteReader();stringj="";while(dr.Read())j+=",'"+Js(dr[0].ToString())+"'";dr.Close();cm.Dispose();rStr="success:true,data:["+(j==""?"":j.Substring(1))+"]";}catch(Exceptione){rStr="success:false,err:'发生以下错误"+Js(e.Message)+"'";}cn.Close();}returnrStr;}}
<!DOCTYPE html> 
<html> 
  <head> 
    <meta charset="UTF-8"> 
    <title>微博留言板</title> 
    <style type="text/css"> 
      *{ 
        margin: 0; 
        padding: 0; 
      } 
      #box{ 
        width: 600px; 
        /*height: 500px;*/ 
        border: 2px solid rgb(85,85,85); 
        border-radius: 15px; 
        margin: 50px auto; 
        padding: 20px 10px 15px; 
        background-color: rgb(85,85,85); 
      } 
      #content{ 
        display: block; 
        resize: none; 
        width: 550px; 
        height: 200px; 
        margin: 0 auto; 
        border: 2px solid rgb(225,225,225); 
        border-radius: 10px; 
        text-align: center; 
        font-size: 30px; 
        background-color: rgb(225,225,225); 
      } 
      #content:focus{ 
        outline: none; 
        border: 2px solid rgb(225,225,225); 
        box-shadow: 0 0 15px rgb(225,225,225); 
      } 
      #btn{ 
        border: 2px solid rgb(255,204,0); 
        width: 80px; 
        height: 40px; 
        border-radius: 5px; 
        margin-top: 30px; 
        font-size: 17px; 
        cursor: pointer; 
        outline: none; 
        background-color: rgb(255,204,0); 
      } 

      .list{ 
        list-style: none; 
        background-color: rgb(249,249,249); 
        margin-top: 20px; 
      } 
      .list>li{ 
        padding: 20px 10px 10px; 
        border-bottom: 2px solid rgb(68,68,68); 
        font-size: 20px; 
        color: rgb(200,214,225); 
        position: relative; 
        word-break: break-word; 
        word-wrap: break-word; 
        background-color: rgb(85,85,85); 

      } 
      .list>li>.control{ 
        position: absolute; 
        bottom: 3px; 
        right: 5px; 
        font-size: 14px; 
      } 
      .list>li>p{ 
        margin-bottom: 25px; 
      } 
      .control span,.control em{ 
        display: inline-block; 
        margin-right: 15px; 
      } 
      .control em{ 
        color: darkblue; 
        cursor: pointer; 
      } 
      a{ 
        text-decoration: none; 
        color: darkred; 
      } 
      #page>a{ 
        display:inline-block; 
        width: 40px; 
        height: 30px; 
        margin-top: 10px; 
        text-align: center; 
        line-height: 30px; 
        font-size: 20px; 
        border-radius: 5px; 
        color: white; 
        background-color: rgb(51,21,70); 
      } 
      #head{ 
        color: rgb(200,214,225); 
        font-size: 30px; 
        height: 50px; 
        border-bottom: 2px solid rgb(68,68,68); 
        margin-bottom: 20px; 
      } 
    </style> 
  </head> 
  <body> 
    <div id="box"> 
      <div id="head"> 
        留言板 
      </div> 
      <div id="fill_in"> 
        <textarea id="content"></textarea> 
        <button id="btn">提交留言</button> 
      </div> 
      <!--留言列表--> 
      <div id="message_text"> 
        <ul class="list"> 
        </ul> 
      </div> 
      <!--分页--> 
      <div id="page"> 
        <a href="javasript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1</a> 
        <a href="javasript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" >2</a> 
      </div> 
    </div> 
  </body> 
  <script src="Jq/jquery-3.1.1.min.js"> 
</html> 

下一篇:没有了
友情链接: 网站地图
Copyright © 2015-2019 http://www.zen-40.com. mgm美高梅有限公司 版权所有