0

I have file file controller

public function informationAction(){
    $data = $this->getRequest()->getPost();
    $id = (int)$data['courseId'];
    $modelCourse = Mage::getModel('course/courses')->load($id);
    $this->_redirect('course/manage/courses');
    return;   
}

I want use ajax

var $j = jQuery.noConflict();
$j("#information-part").click(function(e){
    e.preventDefault();

    var courseId = $j('#courseId').attr('value');

    $j.ajax({
        type: "POST",
        dataType: "JSON",
        url: "http://hungnguyen-magento.local/course/manage/information",
        data: {
            courseId : courseId
        },
        success: function(response){
            var html = '';
            console.log(response);
        }
    });
});

How i can select data from controller?

2 Answers 2

1

try this

public function informationAction(){
        $data = $this->getRequest()->getPost();
        $id = (int)$data['courseId'];
        $modelCourse = Mage::getModel('course/courses')->load($id);
        $dataCourse = $modelCourse->getData();
        $this->getResponse()->setBody(json_encode($dataCourse));
        return;   
    }

Ajax

<script>
    var $j = jQuery.noConflict();
    $j("#information-part").click(function(e){
        e.preventDefault();

        var courseId = $j('#courseId').attr('value');

        $j.ajax({
            type: "POST",
            dataType: "JSON",
            url: "http://hungnguyen-magento.local/course/manage/information",
            data: {
                courseId : courseId
            },
            success: function(response){
                alert(html);
            }
        });
    });
</script>
1
  • thank you devidas
    – Tom
    May 22, 2020 at 8:21
0

Ypu mean, You need to pass data from controller to ajax?
For that You need to set json as body in response in controller:

$this->getResponse()->setHeader('Content-type', 'application/json');
$this->getResponse()->setBody($json);

Then in ajax You have easy access to data from controller as response param.
But be sure that data You encode to json are fine. Good idea is to prepare array with needed data and use json_encode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.