Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Ajax (may be with web services) for this chat application
    text
    copied!<p>I want to use Ajax for my chat application instead of refreshing an Iframe every second for new messages. Someone told me to use webservices with ajax. What should I do with my code to use Ajax?</p> <p>Here is the code which shows Iframe which is called every second and Iframe src has an aspx page which displays chat messages</p> <pre><code>&lt;script type="text/javascript"&gt; function refreshConversatio() { document.getElementById('iframeDisplayMessage').src = 'frmMessageDisplay.aspx'; } &lt;/script&gt; &lt;body&gt; &lt;div id="divMessageDisplayPage" style="height: 724px; "&gt; &lt;asp:PlaceHolder ID="ContentPlaceHolderDisplayMessage" runat="server"&gt; &lt;iframe id="iframeDisplayMessage" name="iframeDisplayMessage" width="76%" style="background-color:White;" height="95%" frameborder="0" src="frmMessageDisplay.aspx" onload="document.getElementById('iframeDisplayMessage').contentWindow.scrollTo(0,document.getElementById('iframeDisplayMessage').contentWindow.document.body.scrollHeight)"&gt; &lt;/iframe&gt; &lt;/asp:PlaceHolder&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; setInterval(function () { refreshConversatio(); }, 1000) &lt;/script&gt; &lt;/body&gt; </code></pre> <p>This is the Aspx page which is called in Iframe and it has literal which displays messages</p> <pre><code>&lt;div id="divConversation" style="width: 100%;"&gt; &lt;asp:Literal ID="RecepientConversation" runat="server"/&gt; &lt;/div&gt; </code></pre> <p>And this is Ajax code Which has not been used and I don't Know where and How to use it in my above application</p> <pre><code>&lt;!DOCTYPE HTML&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;!-- Reference to google J-Query api. You can download and add jquery javasripts files to you soln.--&gt; &lt;script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"&gt;&lt;/script&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;script type="text/javascript"&gt; function callService() { //url of your web service $.ajax('May be url of web service to be written here', { beforeSend: function (xhr) { }, complete: function () { }, contentType: 'application/json; charset=utf-8', dataType: 'json', jsonp: 'callback', type: 'POST', error: function (xhr, ajaxOptions, thrownError) { //Function will be called when any error occcured. alet(thrownError); }, success: function (data) { if (!data.d) { //Cound not find data. } else { if (curdata != data.d) { //result of you web method is stored in data.d object. //TODO : Work with you data here. alert(data.d); } } } }); } callService(); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>After postback</p> <pre><code> if (!Page.IsPostBack) { } if(dt.Rows.Count != 0) { showOnPage.Append("&lt;div style='background-color:ALICEBLUE;float:left; width:100%; word-wrap: break-word;font-size:14px;'&gt;&lt;pre&gt;&lt;font color='green'&gt;&lt;b&gt;&lt;div style='background-color:ALICEBLUE; margin-right:410px;'&gt;" + dt.Rows[i][2].ToString() + " Says: &lt;/b&gt;&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div style='background-color:ALICEBLUE;font-size:14px;float: left;width: 410px;margin-left: -410px; word-wrap: break-word;font-size:14px;'&gt;&lt;pre&gt;&lt;font&gt;" + dt.Rows[i][0].ToString() + "&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div style='background-color:ALICEBLUE; word-wrap: break-word;'&gt;&lt;p style='color:#8B8A8A; margin-top:0'&gt;Sent at " + Convert.ToDateTime(dt.Rows[i][1]).ToLongTimeString() + "&lt;/p&gt;&lt;/div&gt;&lt;div style='clear:both;'&gt;&lt;/div&gt;"); } RecepientConversation.Text=showOnPage.ToString(); </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