Wrapping an object around javascript json format, how to get angular to parse it -
when json coming web api wrapped in object called "devices" cannot seem @ data angular 2 observable
when use .json file without devices object wrapped works fine...
if call .json file works display
private _producturl = 'api/devices/devices.json';
but observable code not see add in .devices
private _producturl = 'http://localhost:42822/api/device'; constructor(private _http: http) { } getproducts(): observable<idevice[]> {//observable<iproduct[]> { return this._http.get(this._producturl) .map((response: response) => <idevice[]>response.json()) .do(data => console.log("all: " + json.stringify(data))) .catch(this.handleerror); }
you may have cros issue. on server side, add following entry in response header api/device
:
access-control-allow-origin: *
@component({ selector: 'json-com', template: ` <h2><a [href]="titleurl">{{title}}</a></h2> <div *ngfor="let d of devices"> deviceid:{{d.deviceid}} devicestatus:{{d.devicestatus}} </div>` }) export class jsoncom implements oninit { title = 'stackoverflow.com/q/39113330/1810391'; titleurl = 'http://stackoverflow.com/q/39113330/1810391'; private _producturl = 'http://localhost:42822/api/device'; devices: idevice[] = []; constructor(private _http: http) { } ngoninit() { this.getproducts().subscribe( d => { console.log('getproducts():' + json.stringify(d)); this.devices = d.devices; }, e => console.log('error: ' + json.stringify(e))); } getproducts() { return this._http.get(this._producturl) .do(d => console.log('_http.get():' + json.stringify(d))) .map(r => r.json()) } }
if not work, please give me console log.
Comments
Post a Comment