我试图取代" Hello world!"和#34;你也好!"使用ajax replaceWith。但是我收到了这个错误:
无法读取属性' replaceWith'为null
我在哪里走错了路?
注意:ajax调用与alert(响应);
一起使用index.phtml:
<div id="helloworld">Hello world!</div>
<script type="text/javascript">
jQuery.ajax({
url: "<?php echo $this->getUrl('topperproductqa/index/ajax') ?>",
success: function(response){
$('#helloworld').replaceWith(response);
//alert(response);
}
});
</script>
IndexController.php:
class Topper_ProductQA_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
return $this;
}
public function ajaxAction ()
{
$response = "Hello to you too";
$json = json_encode($response);
$this->getResponse()->setHeader('Content-type', 'application/json');
$this->getResponse()->setBody($json);
}
}
答案 0 :(得分:1)
我发现Magento在prototype.js上使用$,我修复了它:
@InjectMocks