【发布时间】:2019-03-26 23:42:48
【问题描述】:
我相信我从 wikimedia API 得到了一个对象,但是不知道如何解析它以显示。
//app.component.ts
import { Component } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Post } from './post';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Wiki Search';
// readonly ROOT_URL = 'https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=japan&origin=*&format=json';
readonly ROOT_URL = 'https://en.wikipedia.org/w/api.php?format=json&action=query&generator=search&gsrnamespace=0&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch=Harry Potter&origin=*&callback=JSON_CALLBACK';
// https://en.wikipedia.org/w/api.php?action=query&format=json&gsrlimit=15&generator=search&origin=*&gsrsearch=japan"
posts: Observable<any>;
constructor(private http: HttpClient) {}
getPosts(){
this.posts = this.http.get(this.ROOT_URL)
}
}
//app.component.html
<input type="text" placeholder="Search for pages..">
<button (click)="getPosts()">Get Posts</button>
<div *ngFor="let post of posts | async">
{{ post | json }}
</div>
<ul class=filter-select>
<li class="filter-select-list">
<p class="wiki-page"></p>
</li>
</ul>
如果我将 responseType: text 插入响应处理程序,我可以在开发控制台中将返回的数据作为错误读取。
【问题讨论】:
标签: javascript angular get angular7