jQuery: Evaluate script in ajax response

javascript, jquery, json, xml

Solution

You'd rather send JSON, it's way easier to interpret. Example:

// Suppose your response is a string:
// { html: "<p>add me to the page</p>, script:"alert('execute me');" }
var obj = eval( "(" + response + ")" ) ;
eval( obj.script ) ;

Problem

XML responses from my webapp have both HTML to add to the page AND some have a script to run. I'm trying to send back XML from my webapp like: ``` <?xml version="1.0"?> <doc> <html-to-insert> <![CDATA[<p>add me to the page</p>]]> </html-to-insert> <script> <![CDATA[ alert('execute me'); ]]> </script> </doc> ``` What I'm doing now is snapping out the `<html-to-insert>` and `<script>` CDATA, inserting the html into the page and eval'ing `<script>`. I'm looking for criticism on my approach. Any suggestions from anyone?

Original source

Related problems