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:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>jQuery AJAX</title>
<!--<script language="javascript" type="text/javascript" src="inc/scripts.js"></script>-->
<script language="javascript" type="text/javascript" src="inc/jquery-1.2.6-intellisense.js"></script>
<script language="javascript" type="text/javascript">
$(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);
}
});
});
});
</script>
</head>
<body runat="server">
<form id="form1" runat="server">
<div id="result" runat="server" style="margin-bottom:5em;"></div>
<div id="clicker" runat="server" style="cursor:pointer;">Click Here to Refresh</div>
</form>
</body>
</html>
And here is the back-end on test.aspx
:
<WebMethod()> _
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
I have tried $("#result).text(msg.d)
as well as forming a list, String.Format("<li>{0}</li>",RandomKey)
, and adding a break tag String.Format("{0}<br />",RandomKey)
.
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?
I got the information on how to call a page without a ScriptManager from this site.