A
A
Anton Shelestov2017-07-31 20:18:46
1C-Bitrix
Anton Shelestov, 2017-07-31 20:18:46

What encoding to return data in ajax request?

People, I still can’t figure out what encoding and how to correctly return data from the server via ajax.
ajax request:

BX.ajax({
            url: '...................',
            method: 'POST',
            //dataType: 'json',
            cache: false,
            onsuccess: function(data){
              parsed = JSON.parse(data);
              // console.log("Fetched saves list: ");
              console.log(parsed);
            },
            onfailure: function(data){
              console.log('Failed to load saves list.');
              console.log(data);
            }
          });

server code:
require_once($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_admin_before.php");
require_once($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/testpartner.testpartnermodule/include.php");
use Bitrix\TestPartner\TestPartnerModule\TestPartnerModuleListTable;

$rsData = TestPartnerModuleListTable::GetList();

$result = array();

while ($row = $rsData->fetch())
{
    $listItem = array(
        'id' => $row['ID'],
        'name' => $row['NAME']
    );
    array_push($result, $listItem);
}
echo json_encode($result);

that's all because $row['NAME'] in this variable is both Latin and Cyrillic.
If instead of $row['NAME'] we write for example "test english word" then everything is sent normally.
All files in windows-1251 encoding
Please explain why there are problems with Cyrillic?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikolaev, 2017-08-01
@gromdron

Since JSON is encoded with Unicode characters , UTF-8 encoding is the best option.
As a practicing developer on 1C-Bitrix, I strongly recommend that you translate projects into UTF-8.
PS You have a namespace
And should be:
Since you are not Bitrix and you do not need to mislead anyone :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question