Question

This post is related to this Magento2 - How to get JSON response from Controller

What I am trying to do is to format the json response of my custom web API, but it seems \Magento\Framework\Controller\Result\JsonFactory doesn't seem to work in Magento 2.3

Here's my implementation:

/** @var \Magento\Framework\Controller\Result\JsonFactory  */
protected $resultJsonFactory;

public function __construct(
    ...
    \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
    ...
) {
    ...
    $this->resultJsonFactory = $resultJsonFactory;
    ...
}  

And implemented it like this:

$result = $this->resultJsonFactory->create();
$result->setData(['message' => 'test']);
return $result;  

And the result (Just a blank bracket):

[]

Additional solution made:
Tried to implement \Zend_Json::encode() and json_encode but getting this result:

"{\"message\":\"test\"}"

Instead of (expected result/prettified json response):

{
    "message": "test"
}

No correct solution

OTHER TIPS

Try this,

$result = $this->resultJsonFactory->create();
$myArray = [];
$myArray['message'] = 'test';
$result->setData($myArray);
return $result;  

Hope this helps.

In your controller,

protected $jsonFactory;

public function __construct(
      \Magento\Backend\App\Action\Context $context,
      \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
) {
      parent::__construct($context);
      $this->jsonFactory = $jsonFactory;
 }

 public function execute()
 {
     $resultJson = $this->jsonFactory->create();
     $messages = [];
     $messages[] = __('Your message.');
     return $resultJson->setData([
           'messages' => $messages
     ]);
 }

In my extension I post to ajax controller and response is in json

https://github.com/DominicWatts/BackInStock/blob/master/Controller/Index/Submit.php#L107-L110

https://github.com/DominicWatts/BackInStock/blob/master/view/frontend/templates/product/view/addtocart.phtml#L95

Works for me. Bit different to what you've tried so far. I hope it helps. Good luck.

Update

Have you tried accessing getResponse from context

https://github.com/DominicWatts/CustomerApprove/blob/master/Plugin/Magento/Customer/Controller/Account/LoginPost.php

    public function __construct(
        Context $context,
        CustomerRepositoryInterface $customerRepositoryInterface,
        ManagerInterface $messageManager
    ) {
        $this->_request = $context->getRequest();
        $this->_response = $context->getResponse();
        $this->resultRedirectFactory = $context->getResultRedirectFactory();
        $this->resultFactory = $context->getResultFactory();
        $this->customerRepositoryInterface = $customerRepositoryInterface;
        $this->messageManager = $messageManager;
    }

The reason why you get blank brackets is because webapi has its own renderer which is trying to convert whole Magento\Framework\Controller\Result\Json object into json.

You should return just the data instead and renderer will convert it.

Use this

return ['message' => 'test'];

Instead of this

$result = $this->resultJsonFactory->create();
$result->setData(['message' => 'test']);
return $result;
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top