2017-01-19 7 views
2

"Hello world!"를 바꾸려고합니다. ajax replaceWith를 사용하여 "Hello to you too"와 함께. 그러나 나는이 오류가 나타납니다 :null - ajax 호출의 'replaceWith'속성을 읽을 수 없습니다.

Cannot read property 'replaceWith' of 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 : 나는 젠토는 prototype.js에에 $를 사용하고 파악

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); 
    } 
} 
+1

무엇 당신은 잘 작동합니다 : https://jsfiddle.net/dn892efr/. 이 코드가 문제의 원인이 될까요? –

+1

실제로이 요소를 얻는 지 확인하려면이 도구를 사용하십시오. jquery 라이브러리 문제 일 수 있습니다 .console.log ($ ('# helloworld')); ' – lakshay

+0

고마워요! 이로 인해 코드보다 더 자세히 살펴 보았으며 "$"를 사용하는 magento의 "프로토 타입"때문이라는 것을 알아 냈습니다. –

답변

1

, 나는 그것을 고정 :

(function($) { 

    $.ajax({ 
     url: "<?php echo $this->getUrl('topperproductqa/index/ajax') ?>", 
     dataType: "json", 
     success: function(response){ 
      $("#helloworld").replaceWith(response); 
      //alert(response); 
     } 
    }); 

})(jQuery); 
관련 문제