views:

204

answers:

3

I'm doing a simple AJAX post request in jQuery to another page on my site to get an XML document response. I'm putting the response into a pre element with syntax highlighting enabled. The response comes through fine (I can alert it), but it isn't being added to thepre element when I attempt to assign it in the handlResponse function with jQuery.

<head>
<script>
   ...
   function handleResponse(response, status)
   {
      $("#output").text(response);
   }
   $.post(url, data, handleResponse, "text");
   ...
</script>
</head>

...

<pre id="output">
</pre>

Just to be clear, the javascript code above is in jQuery's document ready function. Any help would definitely be appreciated.

+1  A: 

Try using html instead of text...

$("#output").html(response);

EDIT: If you think escaping is the issue, the following should escape it well enough to display...

response = response.replace(/</g, "&lt;");
$("#output").html(response);
Josh Stodola
The problem is that I need to escape the response, as it is XML and would be improperly added to the `pre` element.
localshred
And just to ensure I'm not crazy, I tried that, and it also does not work.
localshred
Just edited me answer
Josh Stodola
+1  A: 

Three things:

  1. Is the response being passed correctly? Check by doing alert of response from within the function itself.

  2. Is the function being called correctly? Check by having it do an alert unrelated to the response variable (alert("Yo!")).

  3. Is the text being inserted correctly? I can tell it should work, but for sake of full debug, add a ternary like this:

    var preText = (response != "") ? response : "The problem is with the reponse";
    
Anthony
I didn't add my debugging in the question above, but i've done string alerts as well as alerting the response, and both work fine.
localshred
Within the function itself? That part is key. The function is getting called and likes the variable, etc....
Anthony
Yes, inside the function.
localshred
And you can set something else inside the `text()` does it show up the way the response is not?
Anthony
Oh, and just for giggles, try adding this after the text is set to response... `alert($("#output").text());` If it still shows the response, maybe the problem is just something CRAZY, like the pre is hidden.
Anthony
A: 

Apparently this is a bad question. I'm using the javascript lib SyntaxHighlighter and had preloaded the syntax highlighting on the pre tag I was attempting to use. My solution was to remove the tag and dynamically create it when the ajax response comes in. This worked well other than the fact that I'm trying to determine how to load the highlighting on a dynamically appended dom element.

Thanks for the responses.

localshred