I created module that include this view:
<form class="add-to-cart-form" action="<?php echo $this->getUrl('testTask/index/AddToCart')?>">
<input title="sku" class="sku-area" name="sku" type="text">
<input type="button" class="add-to-cart-button" value="Add to cart">
</form>
My layout:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="1column">
<head>
<css src="css/custom.css"/>
</head>
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="task" template="Amt_TestTask::firstTask.phtml" />
</referenceContainer>
</body>
</page>
I was creating js file, that include ajax query:
(app/code/Amt/TestTask/view/frontend/web/js/custom.js)
define([
"jquery"
], function ($) {
"use strict";
return function () {
var form = $('.add-to-cart-form');
var url = form.attr('action');
$(document).on('click', '.add-to-cart-button', function () {
var param = form.serialize();
alert(param);
$.ajax({
showLoader: true,
url: url,
data: param,
type: "POST"
}).done(function (data) {
});
})
};
});
And I added requirejs-config.js:
var config = {
map: {
'*': {
custom: "Amt_TestTask/js/custom"
}
}
};
Develop mode is enable. This file was adding in static directory.
pub/static/frontend/Magento/blank/en_US/Amt_TestTask/js
I know, that i can create link to this js file in layout.
Like that: <script src="Webkul_Grid::js/grid.js"/>
but I think, that it must work without it.