how to use ajax in magento backend

how to use ajax in magento backend

July 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to use ajax in magento backend

Resolving AJAX Redirect Issue in Magento Backend

When working with AJAX calls from the Magento backend (via .phtml files), you might encounter an unexpected redirect to the dashboard even when properly sending the form key. This issue can be frustrating, but it’s often due to a misconfiguration or misunderstanding of how Magento handles requests.

Understanding the Problem

The core issue lies in the way Magento processes and validates POST requests. When an AJAX call is made from a .phtml file, Magento treats this as a regular form submission rather than an AJAX request. As a result, it attempts to redirect to the dashboard page after processing the data.

In your provided code snippet:

var dataRecord = j('#newForm').serialize();
var url = "<?php echo $this->getUrl('*/*/addNewColumn') ?>";

j.ajax({
    type: "POST",
    url: url,
    data: {data1: dataRecord}
})
.done(function( msg ) {
    alert(msg);
});

The issue arises because you are not sending the form key with your AJAX payload, which Magento requires for security validation of POST requests.

Resolving the Redirect Issue

To resolve this, you need to ensure that your AJAX call includes the form key and also prevent Magento from redirecting after processing the request. Here’s how to do it:

  1. Include the Form Key in Your AJAX Data

Before sending the data via AJAX, ensure you extract the form key from the page and include it in your data object.

var formKey = j('#formkey').val();

Then, update your data property like this:

data: {form_key: formKey, data1: dataRecord}
  1. Prevent Magento from Redirecting

To prevent the redirect after processing the AJAX request, you need to send a specific parameter called _redirect[0] with a value of 0.

Update your data property:

data: {form_key: formKey, _redirect[0]: 0, data1: dataRecord}
  1. Process the AJAX Response

In your success callback function, you can handle the response as needed. If you want to reload a block or refresh some elements on the page, you can use Magento’s JavaScript API.

.done(function( msg ) {
    alert(msg);
    // Refresh a block (if needed)
    window.setLocation("<?php echo $this->getUrl('*/*/index') ?>");
})

Best Practices for Using AJAX in Magento Backend

When working with AJAX in the Magento backend, always remember to:

  • Include the form key in your AJAX payload.
  • Prevent redirects by including _redirect[0]: 0 in your data.
  • Use Magento’s JavaScript API for interacting with elements on the page.

Conclusion

In this post, we’ve addressed a common issue faced when working with AJAX calls from the Magento backend. By understanding how Magento processes POST requests and following the best practices outlined above, you can effectively resolve the redirect issue and work seamlessly with AJAX in your .phtml files.

For more advanced techniques and detailed explanations of Magento’s JavaScript API, I recommend checking out Laravel Company’s Magento 2 Development Guide. This comprehensive guide provides a deep dive into Magento development, including best practices for working with AJAX in the backend.

Remember that proper form key validation is crucial for ensuring the security of your Magento store. Always ensure you’re following Magento’s guidelines and using their provided mechanisms for handling user input safely.

Happy coding! If you have any other questions or need further clarification on this topic, feel free to leave a comment below.

Security Themes Admin Configuration

Our network