localshred
localshred

Reputation: 2233

Updating DOM element with AJAX return data fails in jQuery

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.

Upvotes: 0

Views: 702

Answers (3)

localshred
localshred

Reputation: 2233

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.

Upvotes: 0

Josh Stodola
Josh Stodola

Reputation: 82513

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);

Upvotes: 1

Anthony
Anthony

Reputation: 37065

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";
    

Upvotes: 1

Related Questions