我想獲取數據,並顯示它在離子2,但我有一個錯誤,我不明白。這是我的JSON數據的網址http://thethinker.com.ng/techwand/usr.php。我只想以列表格式顯示一些數據。 console.log(data);將數據顯示在控制檯中,但在嘗試對數據進行排序時出現錯誤。使用{{item.data.first_name}} {{item.data.last_name}}ionic2如何顯示Json數據?
這是我的錯誤
SyntaxError: Unexpected token ' in JSON at position 1
at JSON.parse (<anonymous>)
at Response.Body.json (http://localhost:8100/build/main.js:59262:25)
at MapSubscriber.project (http://localhost:8100/build/main.js:46400:76)
at MapSubscriber._next (http://localhost:8100/build/main.js:45993:35)
at MapSubscriber.Subscriber.next (http://localhost:8100/build/main.js:15510:18)
at XMLHttpRequest.onLoad (http://localhost:8100/build/main.js:59691:38)
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9655)
at Object.onInvokeTask (http://localhost:8100/build/main.js:4616:37)
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9576)
at r.runTask (http://localhost:8100/build/polyfills.js:3:4831)
這是我的打字稿類
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { RedditService } from '../../app/serve/RedditServices';
import { Http } from '@angular/http';
import { DetailsPage } from '../details/details';
import 'rxjs/Rx';
/**
* Generated class for the RedditPage page.
*
* See http://ionicframework.com/docs/components/#navigation for more info
* on Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-reddit',
templateUrl: 'reddit.html',
})
export class RedditPage {
http: any;
baseUrl: String;
items: any;
constructor(public navCtrl: NavController, private redditservice:RedditService,http: Http) {
this.http= http;
this.baseUrl ="http://thethinker.com.ng/techwand/usr.php";
this.getallpost();
}
ngOnInit(){
this.getallpost();
}
getPosts(category, limit){
this.redditservice.getPosts().subscribe(data => {
this.items =data.data;
});
}
getallpost(){
this.http.get(this.baseUrl).map(res => res.json()).subscribe(data => {
this.items = data.data;
console.log(data);
});
}
}
這是一些JSON數據
{
success: true,
-data: (3)[
-{
first_name: "Kayla",
last_name: "Leftwich",
email: "[email protected]",
gender: "Female",
image: http://dummyimage.com/202x140.png/cc0000/ffffff,
country: "United States",
state: "North Carolina",
phone_number: "1-(704)808-0271",
professional: "Community Outreach Specialist"
},
-{
first_name: "Jeniffer",
last_name: "Concklin",
email: "[email protected]",
gender: "Female",
image: http://dummyimage.com/104x134.png/5fa2dd/ffffff,
country: "United States",
state: "Ohio",
phone_number: "1-(937)878-1803",
professional: "Nuclear Power Engineer"
},
-{
first_name: "Gonzalo",
last_name: "Byk",
email: "[email protected]",
gender: "Male",
image: http://dummyimage.com/112x100.png/dddddd/000000,
country: "United States",
state: "North Carolina",
phone_number: "1-(336)376-6805",
professional: "Account Coordinator"
}
]
}
我正在使用本教程https://www.joshmorony.com/using-http-to-fetch-remote-data-from-a-server-in-ionic-2/但它只適用於他的api,我試圖使用我的
可能是畸形json..print響應'.MAP(RES = > {console.log(res); return res.json()})'並在一些在線格式化器中測試你的json。 –
仍然不能正常工作 – arinze
它會記錄json嗎? –