gpt4 book ai didi

angular - 错误 : Http failure during parsing in Angular

转载 作者:行者123 更新时间:2023-12-04 16:00:27 24 4
gpt4 key购买 nike

我正在学习 Angular,我想在 HTML 页面上显示 JSON 数据。错误是在 Angular 中解析期间的 Http 失败。我不知道为什么请告诉我我的错误并给我链接如何显示多种类型的 JSON 数据

person.component.html

<ul>

<li *ngFor="let address of addresses">FirstName:{{ persons.addresses.City }}</li>
</ul>

人.json

[{
"Addresses": [{

"AddressId":101,
"AddressTypes":["permanent", "temporary", "careof", "native"],
"Address-L1":"space",
"Address-L2":"a.b.road",
"Locality":"airoli(east)",
"City":"Mumbai",
"State":"Maharashtra",
"Country":"India",
"Postalcode":400027
}],

"ContactNumbers" :
[
{

"ContactID": 1,
"ContactType": "Home",
"CountryCode": "+91",
"RegionCode": "022",
"Number":2656568965

}]

}]

person-list.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
// import 'rxjs/add/operator/do'; // for debugging

export class Address{
AddressId:number;
City:string="";

}
/**
* This class provides the NameList service with methods to read names and add names.
*/
@Injectable()
export class PersonListService {

/**
* Creates a new NameListService with the injected HttpClient.
* @param {HttpClient} http - The injected HttpClient.
* @constructor
*/
constructor(private http: HttpClient) {}

/**
* Returns an Observable for the HTTP GET request for the JSON resource.
* @return {string[]} The Observable for the HTTP request.
*/

get(): Observable<Address[]>{
console.log("Inside the get service")
return this.http.get('app/person/person.json')

// .do(data => console.log('server data:', data)) // debug
.catch(this.handleError);

}

/**
* Handle HTTP error
*/
private handleError (error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
const errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
}

person.component.ts

@Component({
moduleId: module.id,
selector: 'sd-person',
templateUrl: 'person.component.html',
styleUrls: ['person.component.css']
})
export class PersonComponent implements OnInit {
errorMessage: string;

addresses: Address[]=[];
constructor(public personListService:PersonListService){}
ngOnInit() {
// console.log(jwt.AuthConfig);
this.getperson();
}

getperson(){

this.personListService.get()
.subscribe(
addresses => this.addresses = addresses,
error => this.errorMessage = <any>error
);
console.log(this.persons);
}
}

最佳答案

这在技术上是无效的 JSON,因为“孟买”之后有尾随逗号

应该是

[{"Addresses": [{ 
"AddressId":101,
"City":"Mumbai"
}]
}]

编辑

更新之后,那也是无效的,因为它需要被包裹在一个对象中,像这样:

{
"Addresses": [{

"AddressId": 101,
"AddressTypes": ["permanent", "temporary", "careof", "native"],
"Address-L1": "space",
"Address-L2": "a.b.road",
"Locality": "airoli(east)",
"City": "Mumbai",
"State":"Maharashtra",
"Country": "India",
"Postalcode": 400027
}],

"ContactNumbers": [{

"ContactID": 1,
"ContactType": "Home",
"CountryCode": "+91",
"RegionCode": "022",
"Number": 2656568965

}]

}

这将创建一个包含 2 个数组的对象:“Addresses”和“ContactNumbers”

关于angular - 错误 : Http failure during parsing in Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50655235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com