Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ImpressPages4:通过ajax发送页面数据的正确方法_Php_Ajax_Json_Impresspages - Fatal编程技术网

Php ImpressPages4:通过ajax发送页面数据的正确方法

Php ImpressPages4:通过ajax发送页面数据的正确方法,php,ajax,json,impresspages,Php,Ajax,Json,Impresspages,我想创建一个ajax页面,我想知道如何正确地创建它?基本上,我只需要获取页面数据,并阻止内容 现在我有了一个快速而肮脏的解决方案,在ipBeforeResponceSent事件中打印json并退出,但这很难看 class Event{ public static function ipBeforeResponseSent($event){ $ajax = ipRequest()->getQuery('ajax'); if ($ajax){

我想创建一个ajax页面,我想知道如何正确地创建它?基本上,我只需要获取页面数据,并阻止内容

现在我有了一个快速而肮脏的解决方案,在ipBeforeResponceSent事件中打印json并退出,但这很难看

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方面:

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

也许最干净的解决方案是只向我的插件控制器发送链接?

我假设如果你有
页面URL
,你也应该能够使用
页面id

如果此功能仅与此特定网站相关,请使用
Application
plugin及其
PublicController.php
,该插件已就位

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的外观如下所示:

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);
        }
    });
}
我还没有测试过。把它作为一个原则。您可以在core中找到多个类似AJAX操作的示例。

正确使用AJAX:

传递两个参数:

sa:“Plugin.Action” securityToken:ip.securityToken(如果您使用javascript)

然后创建控制器操作并返回json响应对象:

返回新的\Ip\Response\Json($data)


以下是所有详细信息

请添加更多有关情况的详细信息。这个AJAX是从哪里发送的?您必须通过AJAX发送哪些页面数据(页面ID?)?等等。添加了有问题的javascrpt@AudriusJankauskasWell问题是我不知道页面id。这就是如何获取页面id ipContent()->getCurrentPage()->getId()@thefobs的方法,您从哪里获取页面URL?如果这是一个当前页面,那么Mangirdas有你的代码片段来获取ID。@AudriusJankauskas好吧,我在CMS中做了一些挖掘,发现我可以将ipRouteAction作业中的控制器更改为我的控制器,实际上保留了原始作业。。。。