views:

209

answers:

2

I've got an AJAX transmission of a variable string in my primary coding language (Visual FoxPro 9) that looks like this:

     AjaxResponse = CREATEOBJECT("Custom")
     lcMessage = "<li class='hello'>Hello</li>"
     AjaxResponse.AddProperty("Reply", lcMessage)
     Response.ContentType = "text/html"
     Response.Write(AjaxResponse.Reply)

While using the jQuery function .ajax() I've created a success: function that looks like this:

        $.ajax({
            url: 'index?Proc=GetUuserHistory',
            dataType: "html",
            success: function(data){
               $("div#history-text").html('<ul>' + data + '</ul>');
            };
         });

My problem is that the text inserting into 'div#history-text' is unformatted and contains the li tags still. I've tried substituting the .text for .prepend, .append, and .html with no luck...is there a way to convert this string back to html format after its been received using Ajax?

+3  A: 

Instead of .text() use .html() like this:

$("div#history-text").html(data);

Make sure that you're inserting the <li> into <ul> (you're currently inserting into a <div>) and this will work, inserting it like this: <div><li>Stuff</li></div>...is invalid HTML, not sure what the display results will be, especially cross-browser.

Edit: for future users, make sure any styles aren't affecting the display of your newly added elements :)

Nick Craver
this doesnt work. Whenever I use the .html() function, nothing gets displayed at all.....not even incorrect unformatted text..its just blank
sadmicrowave
@sadmicrowave - If you're inserting a `<li>` into a `<div>` it's going to be unpredictable, including displaying nothing at all... `<li>` can only be withing a `<ul>` or `<ol>`, nothing else.
Nick Craver
@Nick Craver - I've updated my original post to show what I've tried for my .html() function. please advise
sadmicrowave
@sadmicrowave - If you `alert(data)` what do you get?
Nick Craver
@sadmicrowave - You mean you can actually see the `<li>...</li>` in the browser? I reckon that's the server replacing tags with entities.
karim79
@Nick Craver - <li class='hello'>Hello</li>
sadmicrowave
@karim79 - you probably are reckoning right, I just need to know how to fix it.
sadmicrowave
@sadmicrowave - Can you view the response with firebug or another inspector and see that you're not actually getting `>li<` back, already encoded?
Nick Craver
@Nick Craver - I'm AM getting >li< when I use .text() but NOT when I'm using .html()...so what is my problem?
sadmicrowave
@sadmicrowave - You need to check it using something like firebug, seeing the actual server response...seeing it once javascript has it doesn't tell you much, you want to see the string coming back from the server exactly as it is.
Nick Craver
@et al, sorry I had the css property of the li set to display:none lol
sadmicrowave
A: 

Have you tried using .load() instead of .ajax().

bschaeffer
@bschaeffer - this doesn't work either, rendering no results
sadmicrowave
Why don't you just return a json object and then wrap each object in a `<li>` inside the `$.ajax` success function?
bschaeffer