Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you preserve HTML in a jQuery AJAX request?
    text
    copied!<p>I am working on a project that I want to implement AJAX, and I have decided on jQuery as a JavaScript Library. Here is the HTML:</p> <pre><code>&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;jQuery AJAX&lt;/title&gt; &lt;!--&lt;script language="javascript" type="text/javascript" src="inc/scripts.js"&gt;&lt;/script&gt;--&gt; &lt;script language="javascript" type="text/javascript" src="inc/jquery-1.2.6-intellisense.js"&gt;&lt;/script&gt; &lt;script language="javascript" type="text/javascript"&gt; $(document).ready(function(){ $("#clicker").click(function () { $.ajax({ type: "POST", url: "test.aspx/randomString", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { $("#result").append(msg.d); } }); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body runat="server"&gt; &lt;form id="form1" runat="server"&gt; &lt;div id="result" runat="server" style="margin-bottom:5em;"&gt;&lt;/div&gt; &lt;div id="clicker" runat="server" style="cursor:pointer;"&gt;Click Here to Refresh&lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And here is the back-end on <strong><code>test.aspx</code></strong>:</p> <pre><code>&lt;WebMethod()&gt; _ Public Shared Function randomString() As String Dim KeyGen As RandomKeyGenerator Dim NumKeys As Integer Dim i_Keys As Integer Dim RandomKey As String Dim oRet As New StringBuilder NumKeys = 20 KeyGen = New RandomKeyGenerator KeyGen.KeyLetters = "abcdefghijklmnopqrstuvwxyz" KeyGen.KeyNumbers = "0123456789" KeyGen.KeyChars = 12 For i_Keys = 1 To NumKeys RandomKey = KeyGen.Generate() oRet.AppendLine(String.Format("{0}{1}", RandomKey, ControlChars.NewLine)) Next Return oRet.ToString End Function </code></pre> <p>I have tried <strong><code>$("#result).text(msg.d)</code></strong> as well as forming a list, <strong><code>String.Format("&lt;li&gt;{0}&lt;/li&gt;",RandomKey)</code></strong>, and adding a break tag <strong><code>String.Format("{0}&lt;br /&gt;",RandomKey)</code></strong>. </p> <p>When I run the page it returns as one line, all HTML is shown. What do I need to do to make it render the HTML?</p> <p>I got the information on how to call a page without a ScriptManager from <a href="http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/" rel="nofollow noreferrer">this site</a>.</p>
 

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