【发布时间】:2021-09-15 14:18:23
【问题描述】:
我的状态为 200,但它正在打印消息出现在错误消息 alert("error..."); 中。为什么会这样?
function makeSelect() {
var blouseoption = document.querySelector('input[name="blouseoption"]:checked').value;
var url = "http://dukano.co/sakhidev/retailon/productoption/values";
alert(url);
var jsondata = $j('#customoption').serialize();
alert("jsondata: " + JSON.stringify(jsondata));
$j.ajax({
type : 'POST',
url : url,
data : jsondata,
dataType : 'json',
success : function(response) {
console.log("calling");
console.log(response);
alert("call success");
alert("response data:" + JSON.stringify(response));
if (response.status == 200) {
console.log("yes");
} else if (response.status == "error") {
console.log("no");
}
},
error : function(response) {
alert("error...");
alert("response:" + JSON.stringify(response));
console.log(response);
}
});
}
Magento 的控制器函数返回 json 值
public function valuesAction(){
$blouseoption = $this->getRequest()->getParam('blouseoption');
$sareefinishing = $this->getRequest()->getParam('sareefinishing');
$data = array( 'sfinishing' => $sareefinishing, 'layout' => $this->getLayout());
Mage::dispatchEvent('product_custom_option', $data);
$jsonData = json_encode(array($blouseoption, $sareefinishing));
$this->getResponse()->clearHeaders()
->setHeader('Content-type','application/json',true);
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($jsonData));
$this->getResponse()->sendResponse();
}
【问题讨论】:
-
你提到了一个数据类型为“json”。你确定 ajax 调用正在返回 json 对象,即使它是一个空白对象。这里的 jQuery ajax 期待一个 json 响应。快速检查 put ' text' 而不是 json 作为 dataType 并在调用被打印时运行意味着你遇到了问题。
-
将您的回复设为
error : function(jqXHR,errorThrown,status) {console.log(jqXHR.responseText)} -
我添加了返回 json 值的 php 代码。请检查更新。
-
改了之后这个问题依旧存在?如果是,请按照我之前的评论尝试将 dataType 作为文本而不是 json 并查看调用警报是否打印在控制台上。
-
所以加载你的调试器并确定错误是什么。这个问题既不包含重现步骤(不,Magento PHP 函数代码转储不计算在内)也不包含具体的问题描述。 -1
标签: javascript jquery ajax