从我的控制器中,我将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”,“已删除”:false, “ fields”:[{“ fieldId”:“ 0bc403e9000000000000000000000005c10d”,“ content”:“”},{“ fieldId”:“ 0bc403e9000000000000000000000000000005c10f”,“ content”:“ Fluid”},{“ fieldId”:“ 0bc403e9000000000000000000000005c10e”,“ content“:” Men“},{” fieldId“:” 91c22871-0947-4f63-b067-4290ce18c0a0“,” content“:” Anupam“},{” fieldId“:” 0bc403e9000000000000000000000000000005c111“,” content“:”“} ,{“ fieldId”:“ 0bc403e9000000000000000000000005c110”,“ content”:“”},{“ fieldId”:“ 0bc403e9000000000000000000000005c113”,“ content”:“ All”},{“ fieldId”:“ 0bc403e9000000000000000000000000000005c112”,“ content”:“ “},{” fieldId“:” 0bc403e9000000000000000000000000000005c115“,” content“:”“},{” fieldId“:” 0bc403e90000000 00000000000000005c114“,” content“:”新到货“},{” fieldId“:” 0bc403e9000000000000000000000005c117“,” content“:”没关系“},{” fieldId“:” 0bc403e9000000000000000000000005005116“,” 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 \”:{}}“,”状态“:200,” statusText“:”确定“}
我在做什么错,Magento 2是否存在问题?
答案 0 :(得分:0)
在Magento 2中,$resultJsonFactory
用于以JSON格式返回数据,并且您已经在执行正确的操作。但是,响应进入了AJAX请求的错误功能,因为从控制器开始,您将再次使用JSON编码$contact
数据。因此,只需删除json_encode
,如下所示:
return $this->resultJsonFactory->create()->setData([
'success' => true,
'contact' => $contact,
'message' => $this->messageManager->addSuccessMessage("Successfully updated email preferences")
]);
代替:
return $this->resultJsonFactory->create()->setData([
'success' => true,
'contact' => json_encode($contact),
'message' => $this->messageManager->addSuccessMessage("Successfully updated email preferences")
]);