使用Get而不是Post的Angular Way数据表

时间:2018-07-12 13:45:51

标签: angular datatables angular-datatables

文档显示使用Http Post获取Angular Datatables的数据,但是我更喜欢执行Get。这可能吗?我还没有找到任何示例,而是更改为.get并删除了第三个参数,导致页面完全无法加载。

文档样本:

import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';

class Person {
  id: number;
  firstName: string;
  lastName: string;
}

class DataTablesResponse {
  data: any[];
  draw: number;
  recordsFiltered: number;
  recordsTotal: number;
}

@Component({
  selector: 'app-server-side-angular-way',
  templateUrl: 'server-side-angular-way.component.html',
  styleUrls: ['server-side-angular-way.component.css']
})
export class ServerSideAngularWayComponent implements OnInit {
  dtOptions: DataTables.Settings = {};
  persons: Person[];

  constructor(private http: HttpClient) {}

  ngOnInit(): void {
    const that = this;

    this.dtOptions = {
      pagingType: 'full_numbers',
      pageLength: 2,
      serverSide: true,
      processing: true,
      ajax: (dataTablesParameters: any, callback) => {
        that.http
          .post<DataTablesResponse>(
            'https://angular-datatables-demo-server.herokuapp.com/',
            dataTablesParameters, {}
          ).subscribe(resp => {
            that.persons = resp.data;

            callback({
              recordsTotal: resp.recordsTotal,
              recordsFiltered: resp.recordsFiltered,
              data: []
            });
          });
      },
      columns: [{ data: 'id' }, { data: 'firstName' }, { data: 'lastName' }]
    };
  }
}

1 个答案:

答案 0 :(得分:0)

  1. 为什么要使用闭包?您没有任何范围问题

  2. 那是一个演示服务器,他们可能只是犯了一个错误

  3. 尝试将URL粘贴到我的浏览器中之后,我得到了数据,是的,您可以发出GET请求。