2

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"
}
0

Try this,

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

Hope this helps.

  • same I got blank brackets – fmsthird Jul 19 at 10:15
  • can you try after cache:flush? – Mohit Rane Jul 19 at 10:16
  • sure. I will ..let me check now – fmsthird Jul 19 at 10:16
  • just did it now and getting the same result – fmsthird Jul 19 at 10:17
  • try this $myArray=array("message" => "test"); instead of $myArray = []; $myArray['message'] = 'test'; – Mohit Rane Jul 19 at 10:18
0

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
     ]);
 }
  • just tried it really quick and got the same result – fmsthird Jul 19 at 11:08
  • what is the result you are getting? – aravind Jul 19 at 11:24
  • still, blank brackets – fmsthird Jul 19 at 11:48
  • How you are trying to get that message? – aravind Jul 19 at 11:49
  • I'm using postman – fmsthird Jul 19 at 11:50
0

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;
    }
  • thanks for the answer but I got an error about getResponse() method maybe because I am implementing it in a model, not in a controller. I'm not sure – fmsthird Jul 22 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.