Ich versuche, einen Ajax-Aufruf und übergeben Sie den Dropdown-Wert im Controller und erhalten Sie diese bestimmte verwandte Bestellinformationen in View-Datei. Antwort wird wahr, aber wie man diese Antwort in meiner Ansichtsdatei verwendet.Wie bekomme ich Ajax-Antwort vom Controller in Magento 2
Hier meine phtml Datei ist:
<select id="chartOption">
<option value="">Select Option</option>
<option value="status">Status</option>
<option value="payments">Payments</option>
<option value="browser">Browser</option>
</select>
<input type="button" name="chart_button" value="Generate chart" onclick="click_chart()">
<div id="result"></div>
<script type="text/javascript">
function click_chart() {
var a = document.getElementById("chartOption");
var abc = a.options[a.selectedIndex].value;
data = jQuery(this).serialize();
jQuery.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "Blog/Post/Information",
data: "label=" + abc,
success: function (result) { jQuery('#result').html(result); },
error: function (error) { jQuery('#result').html(error); }
});
}
</script>
Und hier ist mein Controller:
public function execute() {
if (isset($_REQUEST['ajax'])) {
$label = $this->getRequest()->getPost('label');
$_orders = $this->getOrders();
$complete = $pending = $closed = $canceled = $processing = $onHold = 0;
foreach ($_orders as $_order):
$label = $_order->getStatusLabel();
if ($label == 'Complete') {
$complete++;
}
if ($label == 'Pending') {
$pending++;
}
if ($label == 'Closed') {
$closed++;
}
if ($label == 'Canceled') {
$canceled++;
}
if ($label == 'Processing') {
$processing++;
}
if ($label == 'On Hold') {
$onHold++;
}
endforeach;
$orderData = "['Task', 'Hours per Day'],['Complete'," . $complete . "],['Pending'," . $pending . "]],['Processing'," . $processing . "]],['Canceled'," . $canceled . "]";
$arr = array('result' => $orderData);
$jsonData = json_encode(array($arr));
$this->getResponse()->setHeader('Content-type', 'application/json');
$this->getResponse()->setBody($jsonData);
}
}
das funktioniert aber magento entmutigt die Verwendung von Superglobalen wie $ _REQUEST. Wenn Sie sich auf dem Marktplatz bewerben wollen, wird es abgelehnt. –