如何在Angular 2中检索数据?

如何在Angular 2中检索数据?,angular,Angular,我使用的是post方法 这是我的ts文件: viewt(testid) { $('#myModal22').modal('show'); var param2 = { testid:testid, } this.service.get_tquestion(param2).subscribe(data => { this.test_question1 = data; console.log(data);

我使用的是post方法

这是我的
ts
文件:

viewt(testid)
{
    $('#myModal22').modal('show');
    var param2 = {
        testid:testid,
    }
    this.service.get_tquestion(param2).subscribe(data => {
        this.test_question1 = data;
        console.log(data);
        console.log(this.test_question1);
    });
}
还有HTML代码:

<ng-container *ngFor="let v of test_question1">
    <tr >
        <td>{{v.questionid}}</td>
        <td>{{v.questionname}}</td>
    </tr>
</ng-container>
API的回应是:

找不到类型为“object”的不同支持对象“[object]”。NgFor只支持绑定到数组之类的可重用文件

我得到的答复是:

{“代码”:200,“结果”:[{“问题ID”:1034,“问题名称”:“识别以下句子中的形容词。我们午餐带了一些三明治。(硬的),“持续时间”:60}]}


但是我不能用HTML显示。

您正在将API的全部响应传递给
测试问题1
变量。由于response是一个对象,并且不可编辑,所以不能将其与
ngFor
指令一起使用。要解决此问题,例如,您可以从服务返回数据数组:

return this.http.post(this.base_url + "test/getquestion", param, { headers: headers })
    .map((res: Response) => res.json().Results);}
或在订阅内分配响应对象的属性:

this.test_question1 = data.Results;

您正在将整个响应从API传递到
test\u question1
变量。由于response是一个对象,并且不可编辑,所以不能将其与
ngFor
指令一起使用。要解决此问题,例如,您可以从服务返回数据数组:

return this.http.post(this.base_url + "test/getquestion", param, { headers: headers })
    .map((res: Response) => res.json().Results);}
或在订阅内分配响应对象的属性:

this.test_question1 = data.Results;

你应该试试这个,对我有用:

为您效劳

import { Injectable } from '@angular/core';
import { URLSearchParams, Http, HttpModule, Response, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';


@Injectable()
export class AuthService {
constructor(private http: Http) { }

authenticate(user_creds)
{
  //console.log(user_creds);
  const header = new Headers();
  header.append('Content-Type', 'application/x-www-form-urlencoded');
  return this.http.post('http://localhost/valuesassessment/api/login/authenticate_login', user_creds, { headers: header })
    .map((res: Response) => res.json());
}
}
在您的
.ts
中:

import { AuthService } from '../auth.service';

export class LoginComponent implements OnInit {
    session_data: any = {};
constructor( private authService: AuthService ) 
{ 
  // your logic before init
}

authenticate(x)
{
    this.authService.authenticate(x)
                    .subscribe(data => 
                    {
                      if(data.ResponseCode == 411)
                      {
                        // console.log(data.ResponseCode)
                        this.session_data = data;
                      }
                      else
                      {
                        this.session_data = data;
                        // console.log(this.session_data);
                      }
                    })
    this.session.store('header',this.header);
}
请注意,在将响应分配给
会话\u数据之前,我们已将其初始化为类型
any


希望这有帮助。

你应该试试这个,对我有用:

为您效劳

import { Injectable } from '@angular/core';
import { URLSearchParams, Http, HttpModule, Response, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';


@Injectable()
export class AuthService {
constructor(private http: Http) { }

authenticate(user_creds)
{
  //console.log(user_creds);
  const header = new Headers();
  header.append('Content-Type', 'application/x-www-form-urlencoded');
  return this.http.post('http://localhost/valuesassessment/api/login/authenticate_login', user_creds, { headers: header })
    .map((res: Response) => res.json());
}
}
在您的
.ts
中:

import { AuthService } from '../auth.service';

export class LoginComponent implements OnInit {
    session_data: any = {};
constructor( private authService: AuthService ) 
{ 
  // your logic before init
}

authenticate(x)
{
    this.authService.authenticate(x)
                    .subscribe(data => 
                    {
                      if(data.ResponseCode == 411)
                      {
                        // console.log(data.ResponseCode)
                        this.session_data = data;
                      }
                      else
                      {
                        this.session_data = data;
                        // console.log(this.session_data);
                      }
                    })
    this.session.store('header',this.header);
}
请注意,在将响应分配给
会话\u数据之前,我们已将其初始化为类型
any


希望这有帮助。

测试问题1
不是数组。这就是为什么会出现这个错误。。你能把它记录下来并添加到问题中吗?
test\u question1
不是数组。这就是为什么会出现这个错误。。你能把它记录下来并添加到问题中吗。?M get:res.json(…)。结果不是函数为什么
json()
method中有三个点?M get:res.json(…)。结果不是函数为什么
json()
method中有三个点?