Как я могу получить данные через запрос ajax? - PullRequest
0 голосов
/ 24 февраля 2020

Я пытаюсь загрузить свои данные через Ajax:

base. html .twig:

 <button class="button">load table</button>

 <div class="result"></div>

  $('.button').on( 'click', function () {

  $.ajax({
    method:'POST',
    data: {
      "id": id,
    },
    url:'{{ path('table') }}',
    success : function (data) {
      $(".result").html(data.output);
    }
  });
 });

Контроллер:

  /**
  * @Route("/table", name="table", methods={"GET","POST"})
  */

  public function table(Request $request) {

    $id = $request->request->get('id');

     $file = 'files/file.json';

     $input = file_get_contents($file);
     $array = json_decode($input);

    foreach ($array as $key => $value) {
     if($key == "data"){
       $new = array_slice($value, 0, 10);
    }
   }

    $array->data = $new;

     $output = json_encode($array);

    $response = new JsonResponse(
      array(
     'message' => 'Success',
     'output' => $this->renderView('form/table.html.twig', array('output' => $output))), 200);

     return $response;
    }
  }

таблица. html .twig:

  My table:

   Here is my table:

  <table class="table">
  </table>


 <script type="text/javascript">
    var table = $('.table').DataTable({
       "serverSide": true,
       "ajax": '{{ output}}',

    });
</script>

file. json:

{"draw": 1, "recordsTotal": 57, "recordsFiltered": 57, "data": [{"DT_RowId": "row_3", "DT_RowData": {"pkey": 3}, "first_name": "Angelica", "last_name": "Ramos", "position": "System Architect", "office" : "London", "start_date": "9 октября 09", "salary": "$ 2875"}, {"DT_RowId": "row_17", "DT_RowData": {"pkey": 17}, "first_name": " Эштон "," last_name ":" Cox "," position ":" Technical Author "," office ":" San Francisco "," start_date ":" 12th 09 09 "," salary ":" $ 4800 "}, .. .]}

Когда я нажимаю на кнопку, загружается текст «Вот моя таблица». Но я ожидаю, что теперь таблица будет загружена. Но больше ничего не видно на моей странице.

...