Ionic 应用程序从 url json 获取数据



我不知道该怎么做。我尝试了互联网上的几个例子。最好的解决方案是什么?

import { Component } from '@angular/core';
@Component({
selector: 'page-hello-ionic',
templateUrl: 'hello-ionic.html'
})
export class HelloIonicPage {
constructor() {
}
}

来执行此操作

修改 src/app/app.module.ts 并导入

import { HttpModule } from '@angular/http';

并添加到导入中

@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}

之后,您可以从angular使用HTTP模块。

在您的主页中

导入模块并像这样使用它

import { Http } from '@angular/http';
constructor(public navCtrl: NavController, public http: Http) {
let url = "https://www.reddit.com/r/gifs/new/.json?limit=10";
this.http.get(url).map(res => res.json()).subscribe(data => {
console.log(data);
});
}

此外,您可以将返回的 JSON 字符串转换为 JSON 对象

var jsonobject = JSON.parse(data)

或者,您可以使用 IONIC NATIVE HTTP 插件

干杯:D

为此,只需使用以下代码,无需导入任何模块

fetch('https://url.com').then(res => res.json())
.then(json => {

console.log(json)  

});

最新更新