【问题标题】:Get Json object from url (web api) with angular 2 & Typescript使用 Angular 2 和 Typescript 从 url (web api) 获取 Json 对象
【发布时间】:2017-06-06 14:23:20
【问题描述】:

`嗨, 我需要一些帮助才能从 Visual Studio 2015 .net Core、Angular 2 和 Typescript 上的 Web API 获取 JSON 数据。 /wwwroot/libs 中的 Angular2 文件夹。

我正在使用 Angular 2 的 http.get()。 get() 函数的结果显示一个未定义的值...为什么?

这是我的文件:

Folders

[API][2]

CustomersController.cs => 通过http://localhost:45149/api/customers/ 访问 OK => [{...},{...},{...}]

public class CustomersController : Controller
{
    // GET: api/customers 
    [HttpGet]
    public IList<Customer> Get()
    {
        var objects = new List<Customer>
        {
            new Customer { Id = 1, Log = "user01", Name = "user1" },
            new Customer { Id = 2, Log = "user02", Name = "user2" },
            new Customer { Id = 3, Log = "user03", Name = "user3" }
        };
        return objects;
    }
}

customer.ts

export class Customer {
constructor(
    public Id: number,
    public Log: string,
    public Name: string
    ){}
}

customer.service.ts

import { Injectable } from "@angular/core";
import { Http } from "@angular/http";
import { Observable } from "rxjs/Observable";
import { Customer } from "./customer";
import "rxjs/Rx"

@Injectable()
export class CustomerService {
private baseUrl = "http://localhost:45149/api/customers/";  // web api URL
constructor(private http: Http) { }

getCustomers() {
    return this.http.get(this.baseUrl)
        .map(res => <Customer[]> res.json())
        .catch(error => {
            console.log(error);
            return Observable.throw(error);
        });
}}

customer.component.ts

import { Component, OnInit } from '@angular/core';
import { HTTP_PROVIDERS } from '@angular/http';
import { Customer } from './customer';
import { CustomerService } from './customer.service'

@Component({
selector: 'app-api',
template: ` 
<ul class="">
<li *ngFor="let customer of customers">
<span class=""> 
{{customer.Id}} </span> {{customer.Name}}
</li>
</ul>`,
    providers: [
            HTTP_PROVIDERS,
            CustomerService
    ] }) 

    export class CustomerComponent implements OnInit{
    api: string;
    public customers: Customer[];

    constructor(private customerService: CustomerService) {
    this.api = "API Customers"; 
}

ngOnInit() {
    this.customerService.getCustomers()
        .subscribe(
        customers =>
        {
            console.log(this.customers);
            this.customers = customers;
        },
        error => alert("error"));
}}

app.module.ts

// 
*** Imports ***
//

@NgModule({
imports: [BrowserModule, routing], //other modules the app depends on
providers: [CustomerService],
declarations: [AppComponent,
HomeComponent,UploadComponent,CustomerComponent],
bootstrap: [AppComponent] // root component to bootstarp
})

export class AppModule { }

app.component.ts

import { Component } from '@angular/core';

@Component({
selector: 'app-root',
templateUrl: './app/app.component.html' // => SPA
})

export class AppComponent {
title = "App-root"
}

谢谢。

【问题讨论】:

  • 您可以在浏览器的开发人员工具中的网络选项卡上看到“GET”请求??如果没有尝试在 ngModule 中导入“HttpModule”并移除“HTTP_PROVIDERS”
  • 我没有在网络选项卡中看到获取请求(检查元素 => 网络)...我看到的只是我的 Http get() 的响应,它可以到达 URL 但什么也没有在 value => undefined 中,当我将客户的价值初始化为客户时:Customer[] = [];值为“数组[0]
  • 日志语句当前在分配给this.customers之前。如果将 log 语句放在赋值之后会记录什么?
  • @hagner +1 好的,现在我的数据在数组中:imgur.com/a/rYYsN 但 get() 函数似乎有问题
  • 看来您在服务器端的序列化程序以小写形式返回变量。更改序列化程序设置以使用大写字母或更改视图以绑定到小写属性(customer.name)

标签: angular typescript


【解决方案1】:

当您尝试绑定到具有大写名称的属性时,您的服务器似乎设置为使用骆驼大小写序列化响应。

如果您希望将序列化程序设置更改为使用其他格式,请查看以下文章:Web API serialize properties starting from lowercase letter

否则你应该可以绑定到角边的小写名称

【讨论】:

  • 服务器端类(api)相关的类或接口是否必须创建?如果没有,为什么要这样实施? ty
  • 如果您只想显示它,则不必为它创建一个类。如果你愿意,你可以使用类型变量:any 来获取它,然后绑定到模板中的正确属性。但是,只要您希望在组件或指令中以任何方式与它进行交互,以执行排序、过滤或类似操作,您就需要一个类。
猜你喜欢
  • 2018-11-06
  • 2016-03-30
  • 1970-01-01
  • 2017-08-16
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 2021-07-05
  • 2018-01-03
相关资源
最近更新 更多