views:

891

answers:

3

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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.

A: 

Why are you using "json" as dataType, while you are returning html? Use "html" as dataType and replace msg.d with msg

kgiannakakis
When I changed it to HTML/msg, I get this: {"d":"r537pkRW553b\r\n\r\nOzgN1mf3u5 .... <-- a lot more where that came from but definitely not what I am looking for.
Anders
Where does this json object comes from? It isn't evident from the source code you've provided. Are you using a Web Service with json bindings?
kgiannakakis
The reason he's using that specific call syntax is to coerce an ASMX web service to return JSON even though it isn't ASP.NET AJAX making the request. It's necessary.
Dave Ward
+1  A: 

If you need to put the html in a node, for example if you have a div with id=container. You do it like so $('#container').html(htmlContent);. But be careful the response you are getting back from the server shouldn't be html converted to safe strings (with character entities). But you are also using json as a datatype. You should use text for this. Or take the string from a json variable you get from the server and plant it in a node.

Vasil
I just copy/pasted code from the website i referenced in my post. changing the parameters in the query gives me the same result as kg's suggestion
Anders
+3  A: 

Use:

$("#result").html(msg.d)
Dave Ward
I thought I tried that out and it didn't work...thanks
Anders