Using Basic AJAX calls within Magento

Using Basic AJAX calls within Magento

July 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Using Basic AJAX calls within Magento

Title: Solving Simple AJAX Calls Within Magento

Hello developers,

I understand your struggle in setting up a basic AJAX call within Magento. It’s a common issue many of us face due to the complexity of this e-commerce platform. Let me walk you through your current setup and provide a solution.

Current Setup Analysis

Your configuration is almost correct, but there are a few key areas we need to address:

  1. Controller: The Creare/Groupedajax/controllers/AjaxController.php file looks good. You’ve extended it from the core controller and have an action method set up.

  2. Config.xml: Your configuration file is properly set up with the router, layout updates, and module details. This ensures Magento can recognize your module.

  3. Ajax Call: The JavaScript code you provided seems correct as well. You’re using jQuery to post data to the “groupedajax/ajax/index” URL and update the ‘results’ div with the response.

  4. Layout File: The layout/groupedajax.xml file is correctly defining a new layout handle for your AJAX action and assigning it to a template.

  5. Template File (groupedajax.phtml): You mentioned this file has just ‘test’ in it at the moment, which should work fine for testing purposes.

The Issue

The main problem lies in how you are specifying the URL for the AJAX call:

$j.post("groupedajax/ajax/index", { size: $j(this).val()}, function(data) {
        $j('#results').html(data);
    });

Here, “groupedajax/ajax/index” is being treated as a relative path. Magento doesn’t interpret URLs starting with the module name as absolute paths.

The Solution

The solution is simple and elegant: you just need to include a forward slash at the beginning of your URL:

$j.post("/groupedajax/ajax/index", { size: $j(this).val()}, function(data) {
        $j('#results').html(data);
    });

Or better yet, use jQuery’s $.ajax() method with the absolute URL:

$j.ajax({
        url: "/groupedajax/ajax/index",
        type: "POST",
        data: "size="+$j(this).val(),
        success: function(data) {
            $j('#results').html(data);
        }
    });

This tells Magento to treat the URL as absolute, not relative to the current page. The magic is in the forward slash at the beginning.

Conclusion

Remember, when working with Magento’s AJAX functionality, always ensure you are using absolute URLs for your controllers. This ensures that your requests are correctly routed to the proper server-side actions.

If you’re looking for more advanced Magento development tutorials and resources, I highly recommend checking out Laravel Company, especially their section on Magento development best practices.

I hope this helps clarify your issue! Let me know if you have any other questions or need further assistance. Happy coding!

Best, [Your Name]

Database Extensions Themes Configuration

Our network