for some reason it does not work, although everything seems to be written on manuals

<? require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php"); // $APPLICATION->SetTitle("AJAX"); CModule::IncludeModule('form'); function getmaps() { $err_mess = (CForm::err_mess())."<br>Function: GetByID<br>Line: "; global $DB; $strSql = " SELECT * FROM maps "; $res = $DB->Query($strSql, false, $err_mess.__LINE__); return $res; } $maps = getmaps(); echo json_encode($maps); // $APPLICATION->RestartBuffer(); ?> <? require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php"); ?> 

    3 answers 3

    On AJAX pages, it makes no sense to connect the visual parts of the header and footer. Instead of require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");

    type require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");

    Thus, you will connect the service part of the prologue, that is, all functions of the bitrix, but the header will not be displayed. Therefore do not have to apply

    $GLOBALS['APPLICATION']->RestartBuffer();

    Instead

    require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");

    type require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_after.php");

      If you make a page with an Ajax response using echo json_encode($maps); then you need to remove inclusions in the form

       require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php"); require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php"); 
      • And how then will the bitrix work if it is not connected dev.1c-bitrix.ru/api_help/main/js_lib/ajax And the problem is not in ajax, but that the result is empty $ APPLICATION-> RestartBuffer (); - Serge Esmanovich
      • 3
        It is necessary to connect '/bitrix/modules/main/include/prolog_before.php' , it does not contain the visual part - u_mulder

      Everything came to fetch

       <? require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php"); CModule::IncludeModule('form'); function getmaps() { $err_mess = (CForm::err_mess())."<br>Function: getmaps<br>Line: "; global $DB; $strSql = " SELECT F.* FROM maps F "; $res = $DB->Query($strSql, false, $err_mess.__LINE__); return $res; } $maps = getmaps(); $name_array=array(); while ($row = $maps->Fetch()) { array_push($name_array, $row); } print_r($name_array); ?> <? require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php"); ?> 

      C Ajax looks like this

       <? require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php"); $GLOBALS['APPLICATION']->RestartBuffer(); CModule::IncludeModule('form'); function getmaps() { $err_mess = (CForm::err_mess())."<br>Function: getmaps<br>Line: "; global $DB; // $DB = new CDatabase; // $where = "ID = 20"; $strSql = " SELECT * FROM maps LIMIT 0,20"; $res = $DB->Query($strSql, false, $err_mess.__LINE__); $name_array=array(); while ($row = $res->Fetch()) { array_push($name_array, $row); } echo json_encode($name_array); } function getcategory() { $err_mess = (CForm::err_mess())."<br>Function: getmaps<br>Line: "; global $DB; $strSql = " SELECT * FROM maps_category"; $res = $DB->Query($strSql, false, $err_mess.__LINE__); $name_array=array(); while ($row = $res->Fetch()) { array_push($name_array, $row); } echo json_encode($name_array); } if(!empty($_GET['maps'])){ getmaps(); } if(!empty($_GET['category'])){ getcategory(); } die(); ?> <? require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php"); ?>