1

I'm trying to send information from the view (js) to the server. I'm trying this with the JS request:

$.ajax({
    type: 'POST',
    url: 'php_file_in_controller/Main_Class_Name',
    data: data,
    dataType: "json",
    success: function(responseData){
        alert(responseData);
    },
    fail: function(error){
        alert(error);
    }
});

This is my controller:

<?php
 namespace Magento\Checkout\Controller;

 class Main_Class_Name extends \Magento\Framework\App\Action\Action{

 public function execute(){
 ...some code..
 }

} and my config.xml of the module:

<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
    <checkout>
        <options>
            <onepage_checkout_enabled>1</onepage_checkout_enabled>
            <guest_checkout>1</guest_checkout>
        </options>
        <cart>
            <delete_quote_after>30</delete_quote_after>
            <redirect_to_cart>0</redirect_to_cart>
        </cart>
        <cart_link>
            <use_qty>1</use_qty>
        </cart_link>
        <sidebar>
            <display>1</display>
            <count>5</count>
        </sidebar>
        <payment_failed>
            <identity>general</identity>
            <receiver>general</receiver>
            <template>checkout_payment_failed_template</template>
        </payment_failed>
    </checkout>
</default>

The JS file is in the same module as the php file i am trying to send data to. I don't understand why I receive a 404. Is there a more convenient way to pass data from view to controller? If ajax is the way to go (for multiple reasons) what could be my problem? I'm using the checkout module in this situation so routes.xml is not a problem (or is it..?) Thanks in advance.

  • please provide detail code including your config.xml and your controller file – Murtuza Zabuawala Sep 26 '16 at 3:20
  • I added my config.xml and my controller, Thanks – N. Mantel Sep 26 '16 at 5:07

Your Answer

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

Browse other questions tagged or ask your own question.