0

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.

3
  • By the way, if I had to guess one thing, it would be changing the callback to handleResponse(), but I always get messed up on when to use the () and when to leave them out in js, so I always end up doing the wrong one first... Commented Oct 8, 2009 at 20:17
  • Can you give us a sample of what the response text might be? I've just run a mock of this in jsbin.com and it works fine for my example Commented Oct 8, 2009 at 20:21
  • Sorry all, the issue was unfortunately unrelated to jquery and ajax. see my answer below. I have to wait a few days to accept it, so sorry for the confusion. Commented Oct 8, 2009 at 20:30

3 Answers 3

1

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";
    
Sign up to request clarification or add additional context in comments.

4 Comments

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.
Within the function itself? That part is key. The function is getting called and likes the variable, etc....
And you can set something else inside the text() does it show up the way the response is not?
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.
1

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

2 Comments

The problem is that I need to escape the response, as it is XML and would be improperly added to the pre element.
And just to ensure I'm not crazy, I tried that, and it also does not work.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.