HTTP不调用数据

时间:2018-09-25 02:39:44

标签: angular typescript ionic-framework ionic3

我有一个API的三个Get请求,因此我仅通过传递URL来重用了该函数,这使一个与另一个区分开。但是,当我逐步看到提供程序返回数据时,页面响应函数却未接收到它们,它们为null或更具体地为“ JSON的意外结尾”。

页面代码:

carregaNatureza() {
    this.despesaProvider.getComboBoxes('http://xx.xx.xx.xx:xxxx/api/v1/cadastros/natureza/get-dropdownlist/002/001')
    .then((arr_proj) => {

        this.obj_data = arr_proj;

        this.lista_natureza = [];
        for (let i = 0; i < this.lista_natureza.length; i++) {
            this.obj += (
                this.lista_natureza[i]
            )
            this.lista_natureza.push(this.obj);
        }
    })
}

提供者代码:

getComboBoxes(url: string) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json; charset=UTF-8');
    headers.append('Authorization', 'bearer ' + this.global.tokenGlobal);
    let options = new RequestOptions({ headers: headers });

    return this.http.get(url, options)
      .toPromise()
      .then(response => {
          var json_data = (response as any)._body;
          var parsed = JSON.parse(json_data);

          var arr_data = [];

          for (var x in parsed) {
              arr_data.push(parsed[x]);
          }

          return arr_data;
      })
      .catch((error) => {
          var json_error = (error as any)._body;
          var parsed = JSON.parse(json_error);

          var arr = [];

          for (var x in parsed) {
              arr.push(parsed[x]);
          }

          return arr[0];
      });
  }

1 个答案:

答案 0 :(得分:0)

我认为您由于这一行代码而陷入困境。首先尝试console.log(arr_proj)。

  this.obj_data = arr_proj; //u initialize response here
  this.lista_natureza = []; //empty array
  for (let i = 0; i < this.lista_natureza.length; i++) {//checking length of empty array it wont let you come inside because you fail for condition
    this.obj += (
        this.lista_natureza[i]
        ) //rewriteing your actual response 
    this.lista_natureza.push(this.obj);
  }