ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Display HTML Table string in DIV returned by WebMethod using jQuery AJAX<p>Hi <a class="username" rel="iammann"> iammann</a>,</p> <p>You need to change the <span style="text-decoration: underline;">type</span> to POST and <span style="text-decoration: underline;">contentType</span> to application/json and <span style="text-decoration: underline;">dataType</span> to json in the ajax call.</p> <p>And make sure that you have uncomment the below line of codet o allow this Web Service to be called from script using ASP.NET AJAX.</p> <pre class="brush: csharp">[System.Web.Script.Services.ScriptService]</pre> <p>Check the below example.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;div id=&#34;divData&#34;&gt; &lt;/div&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function () { ShowTestMessage(); }); function ShowTestMessage() { $.ajax({ url: &#34;SaubhagyaWebService.asmx/RealTimeData&#34;, type: &#34;POST&#34;, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: OnSuccess, failure: function (response) { }, error: function (response) { alert(response.responseText); } }); } function OnSuccess(response) { document.getElementById(&#34;divData&#34;).innerHTML = response.d; } &lt;/script&gt;</pre> <p><strong><span style="text-decoration: underline;">WebService</span></strong></p> <pre class="brush: csharp">using System; using System.Data; using System.Text; using System.Web.Services; /// &lt;summary&gt; /// Summary description for SaubhagyaWebService /// &lt;/summary&gt; [WebService(Namespace = &#34;http://tempuri.org/&#34;)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class SaubhagyaWebService : System.Web.Services.WebService { [WebMethod] public string RealTimeData() { StringBuilder sb = new StringBuilder(); DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;GeneratorName&#34;, typeof(string)), new DataColumn(&#34;DCData&#34;, typeof(int)), new DataColumn(&#34;SGData&#34;,typeof(int)), new DataColumn(&#34;POINT_VAL&#34;,typeof(int)), new DataColumn(&#34;diff&#34;,typeof(int))}); dt.Rows.Add(&#34;G-1&#34;, 10, 15, 1, 5); dt.Rows.Add(&#34;G-2&#34;, 12, 18, 2, 6); dt.Rows.Add(&#34;G-3&#34;, 15, 10, 3, 5); dt.Rows.Add(&#34;G-4&#34;, 20, 10, 4, 10); if (dt.Rows.Count &gt; 0) { sb.AppendLine(&#34;&lt;table id=&#39;mytbl&#39; class=&#39;table table-striped table-bordered power-table&#39; width=&#39;96%&#39; cellspacing=&#39;0&#39; cellpadding=&#39;0&#39;&gt;&lt;thead&gt;&#34;); sb.AppendLine(&#34;&lt;tr class=&#39;power-table-header&#39;&gt;&lt;th valign=&#39;top&#39; align=&#39;center&#39;&gt;Name of&lt;br&gt;Generator&lt;/th&gt;&lt;th valign=&#39;top&#39; align=&#39;center&#39;&gt;DC&lt;br&gt;&#34;); sb.AppendLine(&#34;(MW)&lt;/th&gt;&lt;th valign=&#39;top&#39; align=&#39;center&#39;&gt;Schedule&lt;br&gt;(MW)&lt;/th&gt;&lt;th valign=&#39;top&#39; align=&#39;center&#39;&gt;Actual&lt;br&gt;(MW)&lt;/th&gt;&lt;th valign=&#39;top&#39; align=&#39;center&#39;&gt;(+) OI/&lt;br&gt;(-)UI&lt;br&gt;(MW)&lt;/th&gt;&#34;); sb.AppendLine(&#34;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&#34;); for (int i = 0; i &lt; dt.Rows.Count; i++) { sb.AppendLine(&#34;&lt;tr&gt;&lt;td&gt;&#34; + dt.Rows[i][&#34;GeneratorName&#34;] + &#34;&lt;/td&gt;&lt;td align=&#39;center&#39;&gt;&#34; + dt.Rows[i][&#34;DCData&#34;] + &#34;&lt;/td&gt;&lt;td align=&#39;center&#39;&gt;&#34; + dt.Rows[i][&#34;SGData&#34;] + &#34;&lt;/td&gt; &lt;td align=&#39;center&#39;&gt;&#34; + dt.Rows[i][&#34;POINT_VAL&#34;] + &#34;&lt;/td&gt;&lt;td align=&#39;center&#39;&gt;&#34; + dt.Rows[i][&#34;diff&#34;] + &#34;&lt;/td&gt;&lt;/tr&gt;&#34;); } sb.AppendLine(&#34;&lt;/tbody&gt;&lt;thead&gt;&lt;tr class=&#39;power-table-header&#39;&gt;&lt;th&gt;TOTAL&lt;/th&gt;&lt;th&gt;&#34; + Convert.ToDouble(dt.Compute(&#34;Sum(DCData)&#34;, string.Empty)) + &#34;&lt;/th&gt;&lt;th&gt;&#34; + Convert.ToDouble(dt.Compute(&#34;Sum(SGData)&#34;, string.Empty)) + &#34;&lt;/th&gt;&lt;th&gt;&#34; + Convert.ToDouble(dt.Compute(&#34;Sum(POINT_VAL)&#34;, string.Empty)) + &#34;&lt;/th&gt;&lt;th&gt;&#34; + Convert.ToDouble(dt.Compute(&#34;Sum(diff)&#34;, string.Empty)) + &#34;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;/table&gt;&#34;); } return sb.ToString(); } } </pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/19wFdFN.jpg" alt="" width="336" height="284" /></span></strong></p>https://www.aspforums.net:443/Threads/983228/Display-HTML-Table-string-in-DIV-returned-by-WebMethod-using-jQuery-AJAX/https://www.aspforums.net:443/Threads/983228/Display-HTML-Table-string-in-DIV-returned-by-WebMethod-using-jQuery-AJAX/Tue, 03 Jul 2018 04:05:32 GMT