1

From my controller, I return a JSON data to my Ajax call but it always goes in the error section, even when it's not an error.

 jQuery.ajax({
        type: 'POST',
        url: '/yxcustomer/index/emailpreferences',
        data: {"category1": category1,"category2":category2 , "category3":category3,"category4":category4,"category5":category5,"category6":category6,"category7":category7,
               "latest1":latest1,"latest2":latest2,"latest3":latest3,"latest4":latest4,
               "frequency":frequency,
               "email":email,"firstName":firstName , "lastName":lastName},
        dataType: "json",
        success: function (data) {
            console.log("data response success prefe " + JSON.stringify(data));

        },
        error: function (error) {
            console.log("data response error prefe " + JSON.stringify(error));
        }
    });

Controller code

 protected $resultJsonFactory;

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


 public function execute()
  {
    try{
       return  $this->resultJsonFactory->create()->setData(['success' => true,'contact'=>json_encode($contact) ,'message' => $this->messageManager->addSuccessMessage("Successfully updated email preferences")]);
    } catch (Exception $e) {
        return  $this->resultJsonFactory->create()->setData(['success' => false,'message' => $this->messageManager->addErrorMessage('Email preferences cannot be updated')]);            
    }
}

Still that success code

$this->resultJsonFactory->create()->setData(['success' => true,'contact'=>json_encode($contact) ,'message' => $this->messageManager->addSuccessMessage("Successfully updated email preferences")])

Always goes in ajax error part

error: function (error) {}

the response is

data response error prefe {"readyState":4,"responseText":"reading contacts with equalto filter\n{\"success\":true,\"contact\":\"{"id":"c74668c8-e886-4592-8950-273a7a6ab72d","email":"an@gmail.com","status":"onboarding","msgPref":"html","source":"api","customSource":"source","created":"2019-03-20T13:10:40-04:00","modified":"2019-03-21T03:44:36-04:00","deleted":false,"fields":[{"fieldId":"0bc403e9000000000000000000000005c10d","content":""},{"fieldId":"0bc403e9000000000000000000000005c10f","content":"Fluid"},{"fieldId":"0bc403e9000000000000000000000005c10e","content":"Men"},{"fieldId":"91c22871-0947-4f63-b067-4290ce18c0a0","content":"Anupam"},{"fieldId":"0bc403e9000000000000000000000005c111","content":""},{"fieldId":"0bc403e9000000000000000000000005c110","content":""},{"fieldId":"0bc403e9000000000000000000000005c113","content":"All"},{"fieldId":"0bc403e9000000000000000000000005c112","content":""},{"fieldId":"0bc403e9000000000000000000000005c115","content":""},{"fieldId":"0bc403e9000000000000000000000005c114","content":"New Arrivals"},{"fieldId":"0bc403e9000000000000000000000005c117","content":"Never Mind"},{"fieldId":"0bc403e9000000000000000000000005c116","content":""},{"fieldId":"0bc403e9000000000000000000000005c0e8","content":""},{"fieldId":"2b0a63f9-cb2d-4fc7-bcc5-06b30b59f8db","content":"singh"}],"numSends":0,"numBounces":0,"numOpens":0,"numClicks":0,"numConversions":0,"conversionAmount":0}\",\"message\":{}}","status":200,"statusText":"OK"}

What am I doing wrong, is there some Magento 2 issue?

  • and what is the error? print the message and edit it to your question – Philipp Sander Mar 21 at 7:58
  • I said no error , it just comes to error section with try block success messge json – summu Mar 21 at 7:59
  • You can see ajax gets try block json(which is a success not error) in error section of ajax block – summu Mar 21 at 8:04
  • Check response header in console ajax request – Shoaib Munir Mar 21 at 8:07
  • 1
    remove message from the response of the controller. also don't json_encode the $contact variable. Everything will be json encoded when the response is sent. – Marius Mar 21 at 8:16

Your Answer

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

Browse other questions tagged or ask your own question.