Mein Controller ruft an, aber wie sende ich Daten und rufe Daten ab?
Meine Vorlage:
<!-- ko -->
<tr class="totals fee excl">
<th class="mark" colspan="1" scope="row" data-bind="text: title"></th>
<td class="amount">
<form data-role="save" data-bind="submit:save" method="post">
<input type="text" name="spendpoints" id="spendpoints"
data-bind="text: getValue()" value="20"/>
<input type="submit" class="submit"/>
</form>
</td>
</tr>
<!-- /ko -->
Mein js:
define(
[
'Company_Module/js/view/checkout/summary/fee',
'mage/storage',
'jquery'
],
function (Component, storage) {
'use strict';
return Component.extend({
/** Your function for ajax call */
save: function (data) {
// fullScreenLoader.startLoader();
storage.post(
'custom/spendpoints/index',
JSON.stringify(data),
true
).done(
function (response) {
/** Do your code here */
alert('Success--' + data);
// fullScreenLoader.stopLoader();
}
).fail(
function (response) {
// fullScreenLoader.stopLoader();
}
);
// return data;
},
/**
* @override
*/
isDisplayed: function () {
return true;
}
});
}
);
Mein Controller:
<?php
namespace Company\Module\Controller\Spendpoints;
class Index extends \Magento\Framework\App\Action\Action {
protected $_pageFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
)
{
$this->_pageFactory = $pageFactory;
$this->resultJsonFactory = $resultJsonFactory;
return parent::__construct($context);
}
public function execute() {
$result = $this->resultJsonFactory->create();
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/spendpoints.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$request = $this->getRequest()->getPostValue();
$logger->info('spendpointsdata'.print_r($request));
$this->_view->loadLayout();
$this->_view->getPage()->getConfig()->getTitle()->set(__(' My Reward Points '));
$this->_view->renderLayout();
}
}
PS: Ich bin diesem Link gefolgt - https://magento.stackexchange.com/a/175850/55981