Почему JSON возвращает двойные строки для каждой строки в resultSet - PullRequest
0 голосов
/ 12 октября 2019

У меня есть следующий php-код:

 echo(json_encode(array('msg'=>'7','result'=>$list)));

он возвращает следующий json с ajax:

 msg    7
 result […]
        0   {…}
        0   2
        1   Family, Release
        2   test 2 v/s test 3
        3   this is the test for judgment file 2
        4   null
        5   2019-10-10 10:10 PM
        6   1
        7   null
        8   1
        sno 2
        keywords    Family, Release
        case_title  test 2 v/s test 3
        law this is the test for judgment file 2
        judgment_file   null
        added_on    2019-10-10 10:10 PM
        added_by    1
        user_ip null
        is_confirmed    1
        1   {…}
        0   3
        1   Family
        2   Test 3 V/S Test 4
        3   this is tthird record
        4   null
        5   2019-10-10 10:10 PM
        6   1
        7   null
        8   1
        sno 3
        keywords    Family
        case_title  Test 3 V/S Test 4
        law this is tthird record
        judgment_file   null
        added_on    2019-10-10 10:10 PM
        added_by    1
        user_ip null
        is_confirmed    1

Проблема № 1 = он возвращает результат одной строки дважды, т.е. с 0, 1, ... 8 в то время как с именами полей таблицы, т.е. sno = 2 ключевые слова = xxx

Мне нужно получить один результат для каждой строки.

Проблема № 2 = как получить всеполя из json с javascript / jquery

Кто-нибудь поможет?

1 Ответ

0 голосов
/ 12 октября 2019

$list сам содержит дубликаты, и это то, что возвращает PHP. Вы должны исключить дубликаты из $list массива / объекта / массива объектов в php.

См. Этот рабочий пример:

index.php

<button class="button">Get JSON response</button>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
    $('.button').on('click', function (e) {
        $.ajax({
            url: "test.php",
            method: "POST",
            dataType: "json"
        }).done(function (msg) {
            console.log(msg);
        }).fail(function (jqXHR, textStatus) {
            console.log('Request failed: ' + textStatus);
        });
    });
</script>

ajax.php

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $list = new stdClass();
    for ($i = 0; $i < 10; $i++) {
        $list->{'name' . $i} = 'Value ' . $i;
    }

    header('Content-Type: application/json');
    echo(json_encode(array('msg' => '7', 'result' => $list)));
}
?>

Результат:

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...