title: Ajax call always returns in error from controller Magento 2
body: <p>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.</p>
  
  <pre><code>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));
      }
  });
  </code></pre>
  
  <p><code>Controller code</code></p>
  
  <pre><code> protected $resultJsonFactory;
  
  
  
   public function __construct(
      \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
      \Magento\Framework\App\Action\Context $context
  ) {
      $this-&gt;resultJsonFactory = $resultJsonFactory;
      parent::__construct($context);
  }
  
  
   public function execute()
    {
      try{
         return  $this-&gt;resultJsonFactory-&gt;create()-&gt;setData(['success' =&gt; true,'contact'=&gt;json_encode($contact) ,'message' =&gt; $this-&gt;messageManager-&gt;addSuccessMessage("Successfully updated email preferences")]);
      } catch (Exception $e) {
          return  $this-&gt;resultJsonFactory-&gt;create()-&gt;setData(['success' =&gt; false,'message' =&gt; $this-&gt;messageManager-&gt;addErrorMessage('Email preferences cannot be updated')]);            
      }
  }
  </code></pre>
  
  <p>Still that success code</p>
  
  <pre><code>$this-&gt;resultJsonFactory-&gt;create()-&gt;setData(['success' =&gt; true,'contact'=&gt;json_encode($contact) ,'message' =&gt; $this-&gt;messageManager-&gt;addSuccessMessage("Successfully updated email preferences")])
  </code></pre>
  
  <p>Always goes in ajax error part</p>
  
  <pre><code>error: function (error) {}
  </code></pre>
  
  <p>the response in Ajax is</p>
  
  <blockquote>
    <p>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"}</p>
  </blockquote>
  
  <p>What am I doing wrong, is there some Magento 2 issue?</p>