6

I have a custom module, which uses for email varification in frontend of magento. This module will appear as a popup in homepage while it loads. I want to do some other functionings such as display register form etc. in the same popup section, according to the output obtain from this email varification form...

Email varification happens in controller and it look like this

class Realuk_Login_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{

    $this->loadLayout();
    $this->renderLayout();
}
public function loginAction() 
{ 
    $session = Mage::getSingleton('customer/session'); 
    //check whether session exist or not
    if ($session->isLoggedIn()) 
    { 
        return;
    } 

    $result=array('error'=>'noError','success'=>false);
            /*
                email varification code comes here; according to this section
                $array will set;

            */
            //json encoding happens here
            $jsonData=$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result)); /
    $this->_redirect('home'); //this is my home page redirection
}
}  

my frontend file that render popup section contains this code..

<?php
       $array = Mage::helper('core')->jsonDecode($jsonData);
       if(!empty($array))
       {
            echo "<pre>";
            print_r($array);
            echo "</pre>";
       }
       else
       {
            echo "no json data exists";
       }
 ?>

It seems that $array is empty every time in frontend. However in controller $jsonData posses some values. My url look likes www.mydomain.com/home/ and no json value can see there

My question is..

1)Is this the proper way to get results from controller to frontend?

2)How to accomplish json decoding in frontend phtml file ?

Please share your thoughts.. It will help me and others also.

Thanks in advance

4
  • Am I assuming correctly that this popup has a form where the user fills out their email adress which is then submitted with ajax to be verified in your controller?
    – Sander Mangel
    Mar 5 '14 at 13:18
  • @SanderMangel: I am not using ajax for submitting. form is submitting as post data. It will be then varify through controller... Mar 5 '14 at 16:26
  • In that case how does the variable $jsonData with the JSON get to the popup on the homepage
    – Sander Mangel
    Mar 5 '14 at 16:37
  • I am not good with json data.. I though the json encoding code on indexController.php will set json data in url and there by I can access it in front end. I know that, the method I have used in frontend part is wrong.. but I dont know how to decode it? Also there is no url change... url redirection is only happening here Mar 5 '14 at 16:55
20

The proper way to send JSON data from the controller action is to do as such:

public function indexAction()
{
        $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
        $this->getResponse()->setBody(json_encode($response));
}

...where $response is an object or an array.

However, the proper way to "consume" it or to use it depends on your application. The JSON 'decoding' is handled by your ajax method in the javascript. Here are a couple examples:

jQuery

$.ajax({
    dataType: 'json',
    data: /* your data serialized */
    url: '/route/controller/action'
}).done(function(response){
    console.log(response); // will return the json as an object
});

Prototype.js

new Ajax.Response({
    requestHeaders: {Accept: 'application/json'},
    onSuccess: function(response){
        var json = response.responseText.evalJSON(true);
        console.log(json);
    }
});

You can learn more about how to use Prototype's Ajax method in this screencast I produced: http://quick.as/7x6ska7 [end: shamelessplug]

1
  • Pfew! Finally managed to get same JSON data out of Magento. Thank you! Other tutorials about Magento AJAX are so hard to understand... Ps, I used angular's $http service on the receiving end. Jul 23 '16 at 16:04
2

In Realuk_Login_IndexController::loginAction() you're setting response body but response itself is never send to the client, instead you're redirecting to the home page. You can either send the response by using:

$this->getResonse()->sendResponse();

or pass json data to the home page:

$jsonData = array(
    'json' => Mage::helper('core')->urlEncode(
        Mage::helper('core')->jsonEncode($result)
));
$this->_redirect('home', $jsonData);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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