С моего контроллера я возвращаю данные JSON в свой вызов Ajax, но они всегда идут в разделе об ошибках, даже если это не ошибка.
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')]);
}
}
Тем не менее этот код успеха
$this->resultJsonFactory->create()->setData(['success' => true,'contact'=>json_encode($contact) ,'message' => $this->messageManager->addSuccessMessage("Successfully updated email preferences")])
Всегда идет в части ошибки ajax
error: function (error) {}
ответ в Ajax
ошибка ответа данных предпочтение {"readyState": 4, "responseText": "чтение контактов с равным фильтром \ n {\" success \ ": true, \" contact \ ": \" {"id": "c74668c8 -e886-4592-8950-273a7a6ab72d», "электронная почта": "an@gmail.com", "статус": "адаптационный", "msgPref": "HTML", "источник": "API", "customSource": "источник", "создал": "2019-03-20T13: 10: 40-04: 00", "модифицированный": "2019-03-21T03: 44: 36-04: 00", "удален" ложь, "полей": [{ "fieldId": "0bc403e9000000000000000000000005c10d", "контент": ""}, { "fieldId": "0bc403e9000000000000000000000005c10f", "содержание": "Жидкость"}, { "fieldId": "0bc403e9000000000000000000000005c10e",» содержание ":" Мужчины "}, {" fieldId ":" 91c22871-0947-4f63-b067-4290ce18c0a0" , "содержание": "Anupam"}, { "fieldId": "0bc403e9000000000000000000000005c111", "содержание": ""} , { "fieldId": "0bc403e9000000000000000000000005c110", "содержание": ""}, { "fieldId": "0bc403e9000000000000000000000005c113", "содержание": "Все"}, { "fieldId": "0bc403e9000000000000000000000005c112", "содержание":» "}, {" fieldId ":" 0bc403e9000000000000000000000005c115" , "содержание": ""}, { "fieldId": "0bc403e90000 00000000000000000005c114 "," content ":" New Arrivals "}, {" fieldId ":" 0bc403e9000000000000000000000005c117 "," content ":" Never Mind "}, {" fieldId ":" 0bc403e90000000000000000 "" content "11c "fieldId": "0bc403e9000000000000000000000005c0e8", "содержание": ""}, { "fieldId": "2b0a63f9-cb2d-4fc7-bcc5-06b30b59f8db", "содержание": "Singh"}], "numSends": 0,» numBounces ": 0," numOpens ": 0," numClicks ": 0," numConversions ": 0," conversionAmount ": 0} \", \ "сообщение \": {}} " "статус": 200," его статус ":" OK "}
Что я делаю не так, есть проблема с Magento 2?