我尝试将数据插入到由ajax发布为json数据的数据库中。
这是我的HTML:
<form action="" id="crmore_checkout_betting_form">
<input type="checkbox" name="bet[betting_id]" id="bettingID_1" value="1">
<input type="checkbox" name="bet[betting_id]" id="bettingID_2" value="2">
<input type="checkbox" name="bet[betting_id]" id="bettingID_3" value="3">
</form>
这是我的ajax,我使用ajax从上面的表单中发布数据:
<script>
require(
[
'jquery',
'Magento_Ui/js/modal/modal'
],
function(
$,
modal
) {
var options = {
type: 'popup',
responsive: true,
innerScroll: true,
title: 'Bet list',
buttons: [{
text: $.mage.__('Place Bet'),
class: '',
click: function () {
//this.closeModal();
var values = '';
values = $('#crmore_checkout_betting_form').serialize();
$.ajax({
showLoader: true,
url: "../../betting/placebet/place",
data: values,
type: "POST",
dataType: 'json'
}).done(function (data) {
alert(data);
});
}
}]
};
var popup = modal(options, $('#betting_form_popup'));
$(document).ready(function(){
$('#betting_place_button').click(function(){
$('#betting_form_popup').modal('openModal');
});
});
}
);
</script>
这是我想要从ajax接收数据的控制器: 我在这里使用JsonFactory,但我不知道它是对的。
<?php
namespace Crmore\Betting\Controller\Placebet;
use Magento\Framework\App\Action\Context;
class Place extends \Magento\Framework\App\Action\Action{
protected $jsonResultFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory
) {
parent::__construct($context);
$this->jsonResultFactory = $jsonResultFactory;
}
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Json $result */
$result = $this->jsonResultFactory->create();
// here is where I want to write the code to simply display data posted by ajax but I don't know how.
$result->setData(['msg' => __('Bet Placed')]);
return $result;
}
}
请帮忙。