title: Getting XML Data from Ajax Response
body: <p>I'm getting the following XML response back from a Wordpress AJAX request and having trouble extracting the data. I think it's because it's being parsed as CDATA, but I can't see why.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;wp_ajax&gt;
     &lt;response action="mz_mindbody_ajax_add_to_class_0"&gt;
        &lt;object id="0" position="1"&gt;
       &lt;response_data&gt;&lt;![CDATA[error]]&gt;&lt;/response_data&gt;
       &lt;supplemental&gt;
          &lt;classID&gt;&lt;![CDATA[2237]]&gt;&lt;/classID&gt;
          &lt;message&gt;&lt;![CDATA[Error in add to class (2237)]]&gt;&lt;/message&gt;
       &lt;/supplemental&gt;
        &lt;/object&gt;
     &lt;/response&gt;
  &lt;/wp_ajax&gt;
  </code></pre>
  
  <p>This is the php to handle the AJAX call:</p>
  
  <pre><code>&lt;script type='text/javascript'&gt;
  /* &lt;![CDATA[ */
  var my_parameters = {"ajaxurl":"http:\/\/localhost:8888\/wp-admin\/admin-ajax.php"};
  /* ]]&gt; */
  &lt;/script&gt;
  &lt;?php
  
  // Ajax Handler
  add_action( 'wp_ajax_my_ajax_handler', 'my_ajax_handler' );
  function my_ajax_handler() {
      // Get the Post ID from the URL
      $classID = $_REQUEST['classID'];
  
      // Instantiate WP_Ajax_Response
      $response = new WP_Ajax_Response;
  
      if( wp_verify_nonce( $_REQUEST['nonce'], 'nonce-name' . $classID )){
      //Do something here
  
      $response-&gt;add( array(
          'data'  =&gt; 'success',
          'supplemental' =&gt; array(
          'classID' =&gt; 'did it',
          'message' =&gt; 'Class ID goes here', // ideally want to show $classID
          ),
       ) );
      } else {
      // Build the response if an error occurred
      $response-&gt;add( array(
          'data'  =&gt; 'error',
          'supplemental' =&gt; array(
          'classID' =&gt; 'nothing to see here',
          'message' =&gt; 'Error in add to class',
          ),
      ) );
      }
      // Whatever the outcome, send the Response back
      $response-&gt;send();
  
      // Always exit when doing Ajax
      exit();
  }
  //End Ajax
  </code></pre>
  
  <p>This is the jQuery code:</p>
  
  <pre><code>(function($){
  $(document).ready(function($) {
      $( '.my_class' ).click( function( e ) {
          var link = this;
          var id   = $( link ).attr( 'data-id' );
          var nonce = $( link ).attr( 'data-nonce' );
  
          // This is what we are sending the server
          var data = {
              action: 'my_function',
              classID: id,
              nonce: nonce
          }
          // Change text of link
          $( link ).text( 'DOING IT' );
  
          // Post to the server
          $.post( my_parameters.ajaxurl, data, function( data ) {
              // Parse the XML response with jQuery
              // Get the Status
              console.log(data); //the XML data posted above
              var status = $( data ).find( 'response_data' ).text();
              // Get the Message
              var message = $( data ).find( 'supplemental message' ).text();
              // If we are successful, add the success message and remove the link
              console.log(status); // empty string
              if( status == 'success' ) {
              $( link ).parent().after( '&lt;p&gt;&lt;strong&gt;' + message + '&lt;/strong&gt;&lt;/p&gt;').remove();
              } else {
              // An error occurred, alert an error message
              alert( message );
              }
          });
          // Prevent the default behavior for the link
          e.preventDefault();
          });
      }); 
  })(jQuery);
  </code></pre>
  
  <p>In the tutorial I'm following the <code>$response-&gt;add()</code> calls are within a function. Is the fact that mine <em>aren't</em> what's causing the problem here?</p>
  
  <p>Wait - I have misrepresented the <code>data</code> "object", which may be part of (or all of) the problem. The entire data "object" is actually a <em>string</em>, which in the console looks like:</p>
  
  <pre><code>&lt;script type='text/javascript'&gt;
  /* &lt;![CDATA[ */
  var mz_mbo_params = {"ajaxurl":"http:\/\/localhost:8888\/wp-admin\/admin-ajax.php"};
  /* ]]&gt; */
  &lt;/script&gt;
  &lt;?xml version='1.0' encoding='UTF-8' standalone='yes'?&gt;&lt;wp_ajax&gt;&lt;response action='mz_mindbody_ajax_add_to_class_0'&gt;&lt;object id='0' position='1'&gt;&lt;response_data&gt;&lt;![CDATA[error]]&gt;&lt;/response_data&gt;&lt;supplemental&gt;&lt;classID&gt;&lt;![CDATA[nothing to see here]]&gt;&lt;/classID&gt;&lt;message&gt;&lt;![CDATA[Error in add to class]]&gt;&lt;/message&gt;&lt;/supplemental&gt;&lt;/object&gt;&lt;/response&gt;&lt;/wp_ajax&gt;
  </code></pre>