无法读取空的 属性 'replaceWith' - ajax 调用
Cannot read property 'replaceWith' of null - ajax call
我正在尝试使用 ajax replaceWith 将 "Hello world!" 替换为 "Hello to you too"。但是我收到此错误:
Cannot read property 'replaceWith' of null
我哪里走错了?
注意:ajax 调用与 alert(response) 一起工作;
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);
}
}
我发现 Magento 在 prototype.js 上使用了 $,我修复了它:
(function($) {
$.ajax({
url: "<?php echo $this->getUrl('topperproductqa/index/ajax') ?>",
dataType: "json",
success: function(response){
$("#helloworld").replaceWith(response);
//alert(response);
}
});
})(jQuery);
我正在尝试使用 ajax replaceWith 将 "Hello world!" 替换为 "Hello to you too"。但是我收到此错误:
Cannot read property 'replaceWith' of null
我哪里走错了?
注意:ajax 调用与 alert(response) 一起工作;
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);
}
}
我发现 Magento 在 prototype.js 上使用了 $,我修复了它:
(function($) {
$.ajax({
url: "<?php echo $this->getUrl('topperproductqa/index/ajax') ?>",
dataType: "json",
success: function(response){
$("#helloworld").replaceWith(response);
//alert(response);
}
});
})(jQuery);