cancel
Showing results for 
Search instead for 
Did you mean: 

Magento ajax request to controller

Magento ajax request to controller

I have been stuck on trying to send an ajax request to the controller.  I think it is the routing but I don't know where to find the error.  I can Paypal money if you would be able to get on a video call with me to resolve this issue.  I look forward to hearing back from you.

 

Regards,

Che

4 REPLIES 4

Re: Magento ajax request to controller

Hi @che_figueroa,

 

Which version of Magento are you using and how are you trying to send the request?

Re: Magento ajax request to controller

Here is my ajax request:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                <script type="text/javascript">
                    $(document).ready(function(){
                        $(".emailfocusout").focusout(function(){
                            $.ajax({
                                url: "<?php echo $this->getUrl('/checkout/Email/Email') ?>",
                                type: "POST",
                                data: JSON.stringify({"email": "che607@yahoo.com"}),
                                contentType: "application/json",
                                success: function(data) {
                                    console.log("SUCCESS: ", data);
                                },
                                error: function(data) {
                                    console.log('ERROR: ', data);
                                },
                            });
                        });
                    });
                </script>

Here is my controller: 

<?php

class Accutrition_Checkout_EmailController extends Mage_Core_Controller_Front_Action
{
    public function EmailAction()
    {
        $msg = "You hit emailAction";
        echo $msg;
    }
}

Here is my config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Accutrition_Checkout>
            <version>1.1.0</version>
        </Accutrition_Checkout>
    </modules>
    <global>
        <blocks>
            <accutrition_checkout>
                <class>Accutrition_Checkout_Block</class>
            </accutrition_checkout>
            <checkout>
                <rewrite>
                    <onepage_shipping_method_available>Accutrition_Checkout_Block_Onepage_Shipping_Method_Available</onepage_shipping_method_available>
                </rewrite>
            </checkout>
        </blocks>
        <helpers>
            <accutrition_checkout>
                <class>Accutrition_Checkout_Helper</class>
            </accutrition_checkout>
        </helpers>
        <models>
            <accutrition_checkout>
                <class>Accutrition_Checkout_Model</class>
            </accutrition_checkout>
        </models>
    </global>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <Accutrition_Checkout before="Mage_Checkout">Accutrition_Checkout</Accutrition_Checkout>
                    </modules>
                </args>
            </checkout>
        </routers>
        <events>
            <easycheckout_save_address_after>
                <observers>
                    <accutrition_checkout>
                        <class>Accutrition_Checkout_Model_Observer</class>
                        <type>singleton</type>
                        <method>easycheckoutSaveAddressAfter</method>
                    </accutrition_checkout>
                </observers>
            </easycheckout_save_address_after>
            <easycheckout_save_shipping_method_after>
                <observers>
                    <accutrition_checkout>
                        <class>Accutrition_Checkout_Model_Observer</class>
                        <type>singleton</type>
                        <method>easycheckoutSaveShippingMethodAfter</method>
                    </accutrition_checkout>
                </observers>
            </easycheckout_save_shipping_method_after>
            <customer_login>
                <observers>
                    <accutrition_checkout>
                        <class>Accutrition_Checkout_Model_Observer</class>
                        <type>singleton</type>
                        <method>customerLogin</method>
                    </accutrition_checkout>
                </observers>
            </customer_login>
        </events>
    </frontend>
</config>

I know I can send the ajax request because in the ajax request the success function is triggered and I get a 200 code in my post request with the correct params(data: { "email": "che607@yahoo.com", I know because when I check the Network tab in the browser dev tools the right params for the request are there).  I can't hit the controller though.  I am using Magento 1.

Re: Magento ajax request to controller

**UPDATE**

It it working partially.  The base URL is:  http://accutritionp71.local/

When I go to http://accutritionp71.local/checkout/Email/Email it hits the controller and displays the controller action output in the browser.

 

With this request:

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                <script type="text/javascript">
                    $(document).ready(function(){
                        $(".emailfocusout").focusout(function(){
                            $.ajax({
                                url: "<?php echo $this->getUrl('/checkout/Email/Email') ?>",
                                type: "POST",
                                data: JSON.stringify({"email": "che607@yahoo.com"}),
                                contentType: "application/json",
                                success: function(data) {
                                    console.log("SUCCESS: ", data);
                                },
                                error: function(data) {
                                    console.log('ERROR: ', data);
                                },
                            });
                        });
                    });
                </script>

But in the Firefox dev tools it should that the request is: http://accutritionp71.local/

Isn't it supposed to be: http://accutritionp71.local/checkout/Email/Email ?

 

Re: Magento ajax request to controller

I just solved it, I had to change the URL path

 

url: "checkout/Email/Email"