Call controller by ajax but returning html instead of just json data in magento 1.9

Call controller by ajax but returning html instead of just json data in magento 1.9

July 31, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Call controller by ajax but returning html instead of just json data in magento 1.9

Solving Ajax Controller Calling Issues in Magento 1.9

Are you facing challenges when calling a controller via an AJAX function in Magento 1.9? Do you find that instead of receiving the expected JSON data, your response contains the entire HTML page? Let’s dive into this issue and find a practical solution.

Understanding the Problem

In your provided code snippet, you have set up an AJAX request to call a controller. The controller retrieves product data based on category ID and prepares it as JSON. However, when the AJAX function receives the response, it prints the whole HTML page instead of just the JSON data.

The key lies in how Magento handles requests and responses. By default, Magento expects all front-end requests (including AJAX) to return a complete HTML page. This is by design to ensure consistent behavior across all types of requests.

The Root Cause

Your controller code appears to be correct—it clears headers, sets the content type to JSON, and encodes the data as JSON before returning it. However, Magento’s default response handling interferes with this process for AJAX requests.

When an AJAX request is made from a Magento front-end, the system treats it as a regular page load. This means that even though your controller is only returning JSON, Magento wraps that JSON in its standard HTML structure (including layout files, headers, and footers) before sending the response to the browser.

A Practical Solution

To resolve this issue, you need to bypass Magento’s default response handling for AJAX requests. You can achieve this by using a custom controller with an action that specifically handles AJAX responses.

Here’s how to create a new controller:

  1. Create a new file in app/code/local/Mage/Custom/AjaxController.php:
<?php

class Mage_Custom_AjaxController extends Mage_Core_Controller_Front_Action
{
    public function execute()
    {
        // Your existing data retrieval and json encoding code here...
        $json_products = array();

        // Then, use the following to set the response:
        $this->getResponse()->setHttpResponseCode(200);
        $this->getResponse()->setHeader('Content-type', 'application/json');
        $this->getResponse()->setBody(json_encode($json_products));
    }
}
  1. Add a new route in your config.xml file:
<routes>
    <custom_ajax>
        < FrontName>customajax</FrontName>
    </custom_ajax>
</routes>
  1. In your AJAX request, change the URL to point to this new controller:
jQuery.ajax({
    type: 'POST',
    url: "<?php echo $this->getUrl('/customajax/'); ?>",

    success : function(data){
       console.log(JSON.parse(data));
    }
});

Important Notes

  1. Make sure your Magento version supports the local code pool for creating custom modules.

  2. Always clear cache after making changes to route configuration files.

  3. If you need to access session or other Magento-specific data from this controller, ensure you have enabled appropriate security settings in app/etc/modules/Mage_All.xml.

Conclusion

Bypassing Magento’s default response handling for AJAX requests is the key to getting plain JSON responses instead of complete HTML pages. By creating a custom controller and route specifically for AJAX calls, you can maintain the integrity of your application while still utilizing the power of Magento.

Remember that this approach may not be necessary if your AJAX calls are part of an admin area or if you’re using a later version of Magento with improved AJAX handling.

If you found this solution helpful, feel free to explore Laravel Company’s blog for more in-depth articles and real-world examples of Magento development.

Magento 1 Catalog Themes Errors

Our network