【问题标题】:Http Get- Gat an object from web apiHttp Get- 从 web api 获取对象
【发布时间】:2018-12-05 16:28:07
【问题描述】:

我正在尝试从 web api 获取对象列表。 这是服务器代码:

public IQueryable<PersonViewModel> GetPeople()
{
    var People = from p in db.People
                select new PersonViewModel()
                {
                    Id = p.Id,
                    FirstName = p.FirstName,
                    LastName = p.LastName,
                    IdentityNo = p.IdentityNo,
                    DateOfBirth = p.DateOfBirth
                };

    return People;

}

[Route("api/Person/GetPersonById/{value?}")]
public IQueryable<PersonViewModel> GetPersonById(string value = "")
{
    if (value != null)
    {
        var a= GetPeople().Where(x => x.IdentityNo.StartsWith(value));
        return a;
    }
    else return GetPeople();

} 

人物模型:

public class PersonViewModel
{
    public PersonViewModel()
    {

    }
    public PersonViewModel(Person p)
    {
        Id = p.Id;
        FirstName = p.FirstName;
        LastName= p.LastName;
        IdentityNo = p.IdentityNo;
        DateOfBirth = p.DateOfBirth;
    }
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string IdentityNo { get; set; }
    public Nullable<System.DateTime> DateOfBirth { get; set; }
}

客户端代码: 这是模型:

export class Person  {
    Id:string;
    FirstName :string;
    LastName:string;
    IdentityNo:string;
    DateOfBirth:Date;
}

我有一个包含 Person 类型列表的服务, 以及一个试图从服务器获取数据的函数。

PersonIdList: Person[];

getPersonById(uri: string){
    this.http.get('http://localhost:58527/api/Person/GetPersonById/' + uri)
        .map((data: Response) => {
            return data.json() as Person[];
        }).toPromise().then(x => { this.PersonIdList = x; })
}

但是当调试 PersonIdList 未定义时,我在列表中没有得到任何结果。 错误信息:

ReferenceError: PersonIdList 未在 eval 处定义(eval at PersonService.getPersonById(webpack-internal:///./src/app/Shared/person.service.ts), :1:1) 在 PersonService.getPersonById(webpack-internal:///./src/app/Shared/person.service.ts:26:5) 在 MainComponent.onSubmit (webpack-internal:///./src/app/pages/main/main.component.ts:58:28) 在 Object.eval [作为句柄事件] (ng:///AppModule/MainComponent.ngfactory.js:387:31)↵ 在handleEvent (webpack-internal:///./node_modules/@angular/core/esm5/core.js:13805:155)↵ 在 callWithDebugContext (webpack-internal:///./node_modules/@angular/core/esm5/core.js:15314:42)↵ 在 Object.debugHandleEvent [作为句柄事件] (webpack-internal:///./node_modules/@angular/core/esm5/core.js:14901:12)↵ 在调度事件 (webpack-internal:///./node_modules/@angular/core/esm5/core.js:10220:25)↵ 在评估 (webpack-internal:///./node_modules/@angular/core/esm5/core.js:10845:38)↵ 在 HTMLButtonElement.eval (webpack-internal:///./node_modules/@angular/platform-b​​rowser/esm5/platform-b​​rowser.js:2680:53)

【问题讨论】:

  • 服务器是否以正确的 JSON 响应响应?你能把整个服务代码也贴出来吗?
  • 如果您从地图中删除 return 会发生什么?看这个例子:angular-2-training-book.rangle.io/handout/http/… 他们没有在那里使用return

标签: javascript angular typescript asp.net-web-api


【解决方案1】:

这似乎与引用有关, 您可以尝试使用持久的“this”引用,例如 let _this = this;

function getPersonById(uri) {
let _this = this;
this.http.get('http://localhost:58527/api/Person/GetPersonById/' + uri)
    .map(function (data) {
    return data.json();
}).toPromise().then(function (x) { _this.PersonIdList = x; });

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 2012-12-18
    • 2019-02-05
    • 2014-04-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多