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?

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>
| improve this answer | |
  • thank you devidas – Boll May 22 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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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