I'm trying to send some data to my Controller from a template.html using Ajax, so my checkout page does not refresh when I send a custom form, but I don't get anything, the information is an extra field for the sales order, and the fields are empty. This is my code from the template. This code is just after the </form>

<script type="text/javascript">
],function($) {
    $(document).ready(function() {

                submitHandler: function(form) {
                        url: "http://(my server IP)/(folder name)/checkout/Ordercomment/index",
                        data: {comments: 'Test comment from Ajax '},
                        type: 'POST',
                        dataType: 'json',
                        beforeSend: function() {
                            // show some loading icon
                        success: function(data, status, xhr) {
                            // data contains your controller response
                        error: function (xhr, status, errorThrown) {
                            console.log('Error happens. Try again.');

Then this is the part of the controller where I'm trying to receive the data.

namespace Vsourz\Ordercomment\Controller\Ordercomment;
use Magento\Framework\Controller\ResultFactory;

class Index extends \Vsourz\Ordercomment\Controller\Index
    public function execute()

        $response = $this->resultFactory
                'status'  => "ok",
                'message' => "form submitted correctly"

            return $response;

        //This is the field where I want to store the data that comes from Ajax 
 $Ordercomments = $this->getRequest()->getPost('comments');

I hope someone can help me! Thank you!

  • You are returning data before its save, check your controller code. Check the network tab if data is properly sent or not, check the request and response headers. Add those data as well for better solution Jul 17, 2019 at 19:09

1 Answer 1


Use showLoader: true to show a loader

        type: 'POST',
        url: customurl,
        showLoader: true,
        data: {comments: 'Test comment from Ajax'},
        success: function(data)

You can see post data in the browser console

The following line will be first-line controller file.

  • Did you try this? Jul 20, 2019 at 7:25
  • Hello , I tried and did not work, actually it does not send any data and can`t see any message in console. Jul 20, 2019 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.