Note that there are some explanatory texts on larger screens.

plurals
  1. POStack overflow error when using JQuery/ASP.NET for simple ajax chat
    text
    copied!<p>I am trying to create a simple ajax chat using JQuery and ASP.NET. My code works like this: </p> <ol> <li>When the page loads it refreshes the 'chatbox' div with a request to the messages.aspx page, which handles getting new messages from the database and kicks off an auto refresh with the setTimeout().</li> <li>Whenever the user clicks the send button, it adds the message to the database inside the messages.aspx page_load code. </li> </ol> <p>I am getting a stack overflow error right from the start when the timeout starts, and I am not sure what would cause this? Could it be caching? Maybe the code in messages.aspx can't complete running within those 5 seconds? Any help would be appreciated! </p> <p>Also, I didn't worry about sql injection attacks yet b/c I was just trying to get it working with simple code.</p> <p>Here's my code:</p> <p><strong>Client-side:</strong></p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;head runat="server"&gt; &lt;title&gt;Untitled Page&lt;/title&gt; &lt;script src="jquery.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { refreshChat(); $("#btnSend").click(function() { addMessage(); }); return false; }); function refreshChat() { $.get("messages.aspx", function(data) { $("#chatbox").empty(); $("#chatbox").prepend(data); }); setTimeout(refreshChat(), 5000); } function addMessage() { $.get("messages.aspx", {usr: $("#usr").val(), msg: $("#msg").val()} ); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div id="input"&gt; username: &lt;input type="text" name="usr" id="usr" /&gt;&lt;br /&gt; message:&lt;br /&gt; &lt;textarea id="msg" name="msg" rows="5" cols="30"&gt;&lt;/textarea&gt;&lt;br /&gt;&lt;br /&gt; &lt;input type="button" id="btnSend" name="btnSend" value="Send" /&gt; &lt;/div&gt; &lt;div id="chatbox"&gt;&lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Server Side:</strong></p> <pre><code>using System; using System.Data.SqlClient; using System.Configuration; using System.Collections; using System.IO; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class messages : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=F:\\Chatter\\App_Data\\messages.mdf;Integrated Security=True;User Instance=True"); conn.Open(); string sql; SqlCommand comm; if (Request["usr"] != null) { string user = Request["usr"].ToString(); string message = Request["msg"].ToString(); sql = "insert into messages (usr, msg, [date]) values ('" + user + "', '" + message + "', '" + DateTime.Now + "')"; comm = new SqlCommand(sql, conn); comm.ExecuteNonQuery(); } sql = "select top 5 usr, msg from messages order by [date] desc"; comm = new SqlCommand(sql, conn); SqlDataReader dr = comm.ExecuteReader(); while (dr.Read()) { Response.Write(dr["usr"].ToString() + ": &lt;br/&gt;" + dr["msg"] + "&lt;br/&gt;&lt;br/&gt;"); } dr.Close(); conn.Close(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload