1

Followed this tutorial: http://www.atwix.com/magento/ajax-requests-in-magento/

Though dated, people seem to be using it just fine. I'm using Magento CE 1.9.0.1.

After creating app/code/local/MichaelHarper/Upseller/controllers/AjaxController.php:

<?php

// Filename app/code/local/MichaelHarper/Upseller/controllers/AjaxController.php

class MichaelHarper_Upseller_AjaxController extends Mage_Core_Controller_Front_Action {

    public function indexAction() {
        $this->loadLayout();
        $this->renderLayout();
    }
}

And app/code/local/MichaelHarper/Upseller/etc/config.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <MichaelHarper_Upseller>
      <version>0.1.0</version>
    </MichaelHarper_Upseller>
  </modules>
  <frontend>
  <global>
     <blocks>
         <upseller>
              <class>MichaelHarper_Upseller_Block</class>
         </upseller>
      </blocks>
  </global>
    <routers>
      <upseller>
        <use>standard</use>
        <args>
          <module>MichaelHarper_Upseller</module>
          <frontName>upseller</frontName>
        </args>
      </upseller>
    </routers>
    <layout>
      <updates>
        <upseller>
          <file>ajaxlayout.xml</file>
        </upseller>
      </updates>
    </layout>

  </frontend>
</config>

I'm able to go to my local site with controller namespace without error and it loads my theme's header and footer (blank body): http://localstore.dev/upseller/ajax/index

I then create app/design/frontend/mytheme/default/layout/ajaxlayout.xml:

<upseller_ajax_index>
    <reference name="content">
        <block type="upseller/ajaxblock" name="root" template="upseller/default.phtml" />
    </reference>
</upseller_ajax_index>

And finally my template file app/design/frontend/mytheme/default/template/upseller/default.phtml:

<?php 
// app/design/frontend/mytheme/default/template/upseller/default.phtml
echo "Hello World";
?>

Not getting anything when I load my site URL with namespace - still empty body. Any idea what I'm doing wrong? Also, once I can get my Hello World working I have these two provided methods for calling this AJAX controller:

xmlhttp.open("GET","upseller/ajax/index/someval/"+value,true);

-or-

jQuery.post(“upseller/ajax/index”, {someval: value}, function(data){...});

But testing them in the console renders errors.

6
  • where your block class?]
    – Amit Bera
    Commented Sep 15, 2014 at 15:58
  • Just added it into the config.xml file. Wasn't detailed in the tutorial. Also, the post keeps dropping the last two lines of my config.xml file where are: </frontend> </config>
    – sparecycle
    Commented Sep 15, 2014 at 16:11
  • What changes should I make to the ajaxlayout.xml file now that I've defined a block class?
    – sparecycle
    Commented Sep 15, 2014 at 16:12
  • block referece name is missing .it shoul <upseller_ajax_index> <reference name="content"> <block type="upseller/ajaxblock" name="root" template="upseller/default.phtml" /> </reference> </upseller_ajax_index>
    – Amit Bera
    Commented Sep 15, 2014 at 16:16
  • Thank you. Added. Refreshed but still nothing in the body. Is there another way to test/debug this?
    – sparecycle
    Commented Sep 15, 2014 at 16:22

4 Answers 4

1

The name of the layout update handle is created from the node name, in your case upseller, therefore I would try upseller_ajax_index

2
  • Thanks for the fast response. Changed it to <block type="upseller/ajaxblock" name="root" template="upseller/default.phtml" /> </upseller_ajax_index> but still rendering blank page.
    – sparecycle
    Commented Sep 15, 2014 at 15:57
  • You should check app/code/core/Mage/Core/Controller/Varien/Action.php:289 which is the correct layout handle. If the handle is correct, check whether your block is loaded, t hen check the template Commented Sep 15, 2014 at 16:32
1

try by adding output attribute:

   <block type="upseller/ajaxblock" name="root" output="toHtml"  template="upseller/default.phtml" />
6
  • Still blank. I turned on template hints but it doesn't reveal anything. What would be the correct parent path for "upseller/default.phtml"? Maybe I'm not pointing at the right directory.
    – sparecycle
    Commented Sep 15, 2014 at 17:51
  • 1
    is your module active?
    – MTM
    Commented Sep 15, 2014 at 18:32
  • Set to true. Good thing to check though.
    – sparecycle
    Commented Sep 15, 2014 at 18:34
  • do u still get the blank page?
    – MTM
    Commented Sep 15, 2014 at 18:36
  • Yes, sorry. It was set to true. I'm considering closing this question. Maybe there is something wrong with my install or a conflict that isn't obvious.
    – sparecycle
    Commented Sep 15, 2014 at 18:41
1

If you aim is to simply show your block on the ajax request and no other content such as header and footer then your layout.xml should be as follows.

<upseller_ajax_index>
    <block type="upseller/ajaxblock" name="root" output="toHtml" template="upseller/default.phtml" />
</upseller_ajax_index>

The key part there is the output="toHtml" which will actually get your block to show.

A great example of ajax work with layout is the one page checkout. I would suggest looking into the file app/design/frontend/base/default/layout/checkout.xml for any more information.

0

Try

<upseller_ajax_index>
   <reference name="content">
     <block type="upseller/ajaxblock" name="upseller.ajaxblock" template="upseller/default.phtml" />
   </reference>
</upseller_ajax_index>

Please be more consistence with your module and file name

1
  • Still blank. I turned on template hints but it doesn't reveal anything. What would be the correct parent path for "upseller/default.phtml"? Maybe I'm not pointing at the right file?
    – sparecycle
    Commented Sep 15, 2014 at 18:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.