Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Http Get-从web api获取对象_Javascript_Angular_Typescript_Asp.net Web Api - Fatal编程技术网

Javascript Http Get-从web api获取对象

Javascript Http Get-从web api获取对象,javascript,angular,typescript,asp.net-web-api,Javascript,Angular,Typescript,Asp.net Web Api,我正在尝试从web api获取对象列表。 这是服务器代码: public IQueryable<PersonViewModel> GetPeople() { var People = from p in db.People select new PersonViewModel() { Id = p.Id, FirstName = p.

我正在尝试从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();

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

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:在eval时未定义PersonIdList(eval在 PersonService.getPersonById(网页包-internal:///./src/app/Shared/person.service.ts), :1:1)在 PersonService.getPersonById(网页包-internal:///./src/app/Shared/person.service.ts:26:5) 在MainComponent.onSubmit (网页-internal:///./src/app/pages/main/main.component.ts:58:28)在 Object.eval[作为handleEvent] (ng:///AppModule/MainComponent.ngfactory.js:387:31)↵ 手边 (网页-internal:///./node_modules/@角度/核心/esm5/core.js:13805:155)↵ 在callWithDebugContext (网页-internal:///./node_modules/@角度/核心/esm5/core.js:15314:42)↵ 在Object.debugHandleEvent[作为handleEvent] (网页-internal:///./node_modules/@角度/core/esm5/core.js:14901:12)↵ 在调度事件中 (网页-internal:///./node_modules/@角度/core/esm5/core.js:10220:25)↵ 评估时 (网页-internal:///./node_modules/@角度/core/esm5/core.js:10845:38)↵ 在HTMLButtonElement.eval (网页-internal:///./node_modules/@angular/platform browser/esm5/platform browser.js:2680:53)


这似乎与参考有关, 你能尝试保持“this”引用吗,像这样let\u 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; });

}

服务器是否使用正确的JSON响应进行响应?您可以同时发布整个服务代码吗?如果从地图中删除
return
,会发生什么情况?看看这个例子:他们没有在那里使用
return
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; })
}
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; });