3
我嘗試進行ajax調用,並將控制器中的下拉值傳遞給視圖文件並獲取特定的相關訂單信息。響應成真,但如何在我的視圖文件中使用該響應。我如何從控制器在magento 2中獲得ajax響應
這裏是我的PHTML文件:
<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>
這裏是我的控制器:
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);
}
}
這個工程,但magento阻止像$ _REQUEST超級全球的使用。如果你想提交給市場,它將被拒絕。 –