Asp.net 在Angular 8中发出HTTP请求

Asp.net 在Angular 8中发出HTTP请求,asp.net,angular,Asp.net,Angular,我正在用angular发出http请求。但值不会显示在网页中 我还使用了无样本api- 但这不会显示任何值 这是我的组件 @Component({ selector: 'app-value', templateUrl: './value.component.html', styleUrls: ['./value.component.css'] }) export class ValueComponent implements OnInit {

我正在用angular发出http请求。但值不会显示在网页中 我还使用了无样本api- 但这不会显示任何值

这是我的组件

 @Component({
      selector: 'app-value',
      templateUrl: './value.component.html',
      styleUrls: ['./value.component.css']
    })
    export class ValueComponent implements OnInit {

      values: any;
      constructor(private http: HttpClient) { }

      ngOnInit() {
        this.getValues();
      }

      getValues() {
      return this.http.get<any>('https://reqres.in/api/users/2').subscribe(response => {this.values = response.any; });

      }

    }
@组件({
选择器:“应用程序值”,
templateUrl:'./value.component.html',
样式URL:['./value.component.css']
})
导出类ValueComponent实现OnInit{
价值观:任何;
构造函数(私有http:HttpClient){}
恩戈尼尼特(){
这是getValues();
}
getValues(){
返回此.http.get('https://reqres.in/api/users/2').subscribe(response=>{this.values=response.any;});
}
}
这里是component.html

 <ul>
    <li *ngFor="let value of values">
      {{ value.text }}
    </li>
  </ul>
    {{value.text}

如果您得到一条记录,请不要使用ngFor

将response.any更改为response.data,因为api在JSON中返回“data”作为键而不是“any”,然后在模板中添加,例如:

<ul>
    <li>
      {{ values.email}}
    </li>
  </ul>
  • {{values.email}

当api以对象数组的形式返回数据时,可以使用ngFor。非常好的解决方案是使用Postman,检查API返回的数据,了解JSON结构的外观。

如果您得到一条记录,请不要使用ngFor

将response.any更改为response.data,因为api在JSON中返回“data”作为键而不是“any”,然后在模板中添加,例如:

<ul>
    <li>
      {{ values.email}}
    </li>
  </ul>
  • {{values.email}

当api以对象数组的形式返回数据时,可以使用ngFor。非常好的解决方案是使用Postman,检查API返回的数据,了解JSON结构的外观。

this.values=response.any
是错误的,因为您的响应是一个具有属性
数据的对象,而不是
any

同样在这个端点,您得到的是单个用户,而不是用户的集合,因此您应该将变量重命名为
value
not
values

 @Component({
      selector: 'app-value',
      templateUrl: './value.component.html',
      styleUrls: ['./value.component.css']
    })
    export class ValueComponent implements OnInit {

      value: any;
      constructor(private http: HttpClient) { }

      ngOnInit() {
        this.getValue();
      }

      getValue() {
      return this.http.get<any>('https://reqres.in/api/users/2').subscribe(response => {this.value = response.data; });

      }

    }

this.values=response.any
是错误的,因为您的响应是一个具有属性
数据的对象,而不是
any

同样在这个端点,您得到的是单个用户,而不是用户的集合,因此您应该将变量重命名为
value
not
values

 @Component({
      selector: 'app-value',
      templateUrl: './value.component.html',
      styleUrls: ['./value.component.css']
    })
    export class ValueComponent implements OnInit {

      value: any;
      constructor(private http: HttpClient) { }

      ngOnInit() {
        this.getValue();
      }

      getValue() {
      return this.http.get<any>('https://reqres.in/api/users/2').subscribe(response => {this.value = response.data; });

      }

    }

为什么使用“response.any”?Response是一个键为“data”的JSON对象。为什么要使用“Response.any”?响应是一个JSON对象,键为“data”。谢谢。成功了。我是这方面的初学者,非常感谢您的帮助:)没问题:)祝您好运。如果需要带列表,我该如何更改?我应该搬到ngFor吗?是否需要在component.ts中进行任何更改?是的,那么您必须使用ngFor。在组件中,您无需更改任何内容。谢谢。成功了。我是这方面的初学者,非常感谢您的帮助:)没问题:)祝您好运。如果需要带列表,我该如何更改?我应该搬到ngFor吗?是否需要在component.ts中进行任何更改?是的,那么您必须使用ngFor。在组件中,您无需更改任何内容。谢谢您的帮助。已将response.any更改为response.data,并已删除ngFor。现在它可以正常工作了。谢谢:)如果我需要带列表,我该如何更改它?我应该搬到ngFor吗?是否需要对组件.ts进行任何更改?谢谢您的帮助。已将response.any更改为response.data,并已删除ngFor。现在它可以正常工作了。谢谢:)如果我需要带列表,我该如何更改它?我应该搬到ngFor吗?是否需要在component.ts中进行任何更改?