我正在尝试创建ajax购物车,并且只需单击“添加到购物车”即可成功添加产品。但无法在不刷新页面的情况下更新购物车/标题链接。经过调试,发现代码卡在“var json = transport.responseText.evalJSON();” 。我在json响应文本之前和之后使用警报进行调试。
以下是我正在使用的代码:
var oldLabel;
$$('.btn-cart').each( function(el){
oldLabel=el.select("span span")[0].innerHTML;
el.title="<?php echo $this->__('Please wait...'); ?>";
el.select("span span")[0].innerHTML="<?php echo $this->__('Please wait...'); ?>"; });
try {
this.form.request({
parameters: { adeptaajax:'true' },
onSuccess: function(transport){
var json = transport.responseText.evalJSON();
var displayString = json.message;
if(json.success) {
$$('.block-cart').invoke('replace', json.cartsidebar);
$$('.header .links').invoke('replace', json.toplinks);
}
$$('.btn-cart').each( function(el){
el.enable();
el.select("span span")[0].innerHTML=oldLabel;
el.title=oldLabel;
});
alert(displayString);
},
onFailure: function(transport){
$$('.btn-cart').each( function(el){
el.enable();
el.select("span span")[0].innerHTML=oldLabel;
el.title=oldLabel;
});
alert("<?php echo $this->__('There has been a problem adding your product. Please try again later.'); ?>");
}
});