Using 'return' with an ajax callback renders an entire page before my content is loaded I'm developing a module that uses Openlayers maps and Drupal Commerce. When you click on the map, you should see products that relate to those coordinates of where you click. I have this working perfectly. 

I've got a 'foreach' inside of my function that creates each product row:

foreach($alldata as $data) {
    $content = '';
    $date = substr($data['_source']['ver_date'], -4);    
    $content .= '&lt;div class="product-row" data-date="' . $date . '" data-id="' . $id . '" data-scale="' . $data['_source']['scale'] . '"&gt;';

    $titleString = '';
    $titleString .= $data['_source']['longname'] . ' ';
    $trimmedTitle = strlen($titleString) &gt; 80 ? substr($titleString,0,80)."..." : $titleString;
    $content .= '&lt;span class="title"&gt;' . $trimmedTitle . '&lt;/span&gt;';

    $content .= '&lt;/div&gt;';
    print $content;
}


This works exactly like it should and renders the rows of products exactly like I want. 

The problem I am having is that there is an 'Add to Cart' button that was created within another function somewhere else in the site that I have to render in this foreach.

$arr = explode(".", $data['_source']['price']);
$inner_html = '&lt;span class="btn-text"&gt;$'.$arr[0].' Print&lt;/span&gt;&lt;span class="glyphicon glyphicon-shopping-cart"&gt;&lt;/span&gt;';
$add_to_cart_btn = drupal_get_form('myModule_product_cart_button', $inner_html, (object)$data['_source']);
$content .= '&lt;div class="product-cart"&gt;' . drupal_render($add_to_cart_btn) . '&lt;/div&gt;';


The button loses it's Ajax properties when I use 'print $content'. So I changed it to 'return $content' and moved it outside the 'foreach'. 

I get all of the content properly as well as a working ajax 'Add to Cart' button. However, I also get the entire page template rendered prior to all of my content that I'm returning. 

&lt;--- Content Rendered In Div via Ajax ---&gt;

Full Empty Page Template
---------------------------
Ajax content formatted properly and working properly


I've tried using ajax_render. I've tried using ajax_command_html. I've tried completely redoing my function and ajax to use drupal.behaviors. Nothing is working to remove that full page from rendering prior to my content. It only happens I when use 'return'. I've also tried adding drupal_page_footer().

Even when I have absolutely nothing in my foreach, if I use 'return $content', I get a full page rendered. And it's not the use of '$content'. I've changed that to other variable namespaces and it still renders an empty page template.

Here is my ajax call:

 $('.products').mousedown(function() {
    $.ajax({
      url: Drupal.settings.myModule.ajaxUrl, 
      method: 'post',
      data: { 
        longtitle : title,
        id : id 
      }, 
      dataType: "text",          
      success: function(data) {
        var markup = '';
        markup += data;

          $('.products-pane').html(markup);

      }
    });   
  }); 


Anyone know what is causing this?
You are missing the Drupal settings that include the Ajax settings, which is why the server doesn't know it should be ajaxified. You should (may) be able to fix it by changing this:

$content .= '&lt;/div&gt;';
print $content;


To this:

$content .= '&lt;/div&gt;';
$scripts = drupal_add_js();
if (!empty($scripts['settings']))
{
    $settings = '&lt;script type="text/javascript"&gt;jQuery.extend(Drupal.settings, ';
    $settings .= drupal_json_encode(call_user_func_array('array_merge_recursive', $scripts['settings']['data']));
    $settings .= ');&lt;/script&gt;';
}

print $content . $settings;
drupal_exit();