cagartner
12/1/2016 - 1:29 PM

Returning json data on magento 2.1 controller

Returning json data on magento 2.1 controller

<?php

// ... Controller.php

public function execute()
{
  $result = ['message' => 'Success'];
  
  $this->getResponse()->representJson(
      // Use the method jsonEncode from Magento\Framework\Json\Helper\Data Helper
      $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode([
          'result' => $result
      ])
  );
  return;
}