0

I want to create an ajax page and I was wondering how to do it correctly? Essentialy I just need to get page data, and block content.

For now I have quick and dirty solution for the problem, by printing json in ipBeforeResponceSent Event and exiting, but it's ugly..

class Event{
    public static function ipBeforeResponseSent($event){
        $ajax =  ipRequest()->getQuery('ajax');
        if ($ajax){
            $page = ipContent()->getCurrentPage();

            $data['status'] = 'success';
            $data['url'] = $page->getLink();
            $data['page'] = ipContent()->getBlockContent('main');
            $data['title'] = $page->getTitle();
            $data['id'] = $page->getId();

            $data['pageorder'] = $page->getOrder();
            $data['parent'] = $page->getParentId();
            $data['timestamp'] = time();

            exit(json_encode($data, true));
        }
    }
}

Javascript side:

$.getJSON(PAGE_URL, {ajax: 'true'}, function(responce) {
    if (responce.status == 'success'){
        /***/
    }
});

Maybe the cleanest solution would be to just send link to my plugin's controller?

TheFobas
  • 1
  • 1

2 Answers2

1

Correct usage of AJAX:

pass two parameters:

sa: 'Plugin.Action' securityToken: ip.securityToken (if you are in javascript)

then create controller action and return json response object:

return new \Ip\Response\Json($data);

Here is all that info in details http://www.impresspages.org/docs/controller

Mangirdas Skripka
  • 1,647
  • 1
  • 15
  • 14
0

I'm assuming that if you have PAGE_URL, you should be able to use page id, too.

If this functionality is related only with this specific website, use Application plugin and its PublicController.php which is already in place.

public function getPageAjax()
{
    // Allowing only post actions
    ipRequest()->mustBePost();

    // Getting page ID from posted data
    $pageId = ipRequest()->getPost('pageId');

    // Get page object
    $page = ipContent->getPage($pageId);

    if ($page != null) {
        $data = array();

        // Do what you need

        return \Ip\Response\JsonRpc::result(array('data' => $data));
    } else {
        return \Ip\Response\JsonRpc::error("Page not found");
    }
}

Javascript would look something like this:

function applicationGetPageAjax(pageId) {
    var postData = {
        'pa': 'Application.getPageAjax',
        'pageId': pageId,
        'jsonrpc': '2.0'
    };

    $.ajax({
        url: ip.baseUrl,
        data: postData,
        dataType: 'json',
        type: 'POST',
        success: function (response) {
            if (response && response.result) {


                // Do what you want with response
                alert(response.result.data);


            } else if (response && response.error && response.error.message) {
                alert(response.error.message);
            } else {
                alert('Unknown response.');
            }
        },
        error: function (response) {
            alert('Unexpected error.' + response.responseText);
        }
    });
}

I haven't tested it. Use it as a principle. You can find multiple examples of similar AJAX actions in the core.