【发布时间】:2019-07-05 14:25:11
【问题描述】:
我正在尝试从特定的 Wikipedia 页面获取一些文本,但我无法访问它。我试图了解它是如何工作的,但我不知道为什么它仍然没有,因为没有给出错误。
我尝试了几种类型的 URL,例如:https://www.reddit.com/r/gifs/top/.json?limit=105sort=hothttps://gyazo.com/54ee18fe654fcfe2bd5d546b44f85b5d,它返回。 我怀疑它与 CORS 有关,但不知道它是如何工作的。
servicio.service.ts 代码
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class ServicioService {
constructor(public http: HttpClient) {
console.log('service working!');
}
goWiki(userInput): Observable<any>{
console.log('goWiki working!');
//let url = 'https://www.reddit.com/r/gifs/top/.json?limit=105sort=hot';
return this.http.get('https://es.wikipedia.org/w/api.php?action=query&list=search&srsearch='+ userInput +'&utf8=&format=json',);
} //took that link from this: <https://en.wikipedia.org/wiki/Special:ApiSandbox#action=query&format=json&meta=siteinfo&siprop=magicwords>
}
...
home.page.ts
import { ServicioService } from '../servicio.service';
...
constructor(
navcntrlr: NavController,
private alertcontroller: AlertController,
public servicio: ServicioService) { }
userInput = 'alcasser';
ngOnInit(): void {
this.search();
}
search(){
console.log("henllo xd");
const hola = this.servicio.goWiki(this.userInput).
subscribe((response) => {
console.log(response);
});
}
...
我希望得到一个可观察的 json 以便能够使用它并从中获取我需要的信息。
【问题讨论】:
-
如果您将错误回调添加到您的订阅方法,您会看到什么?
-
控制台有什么错误? (包括 cors 警告)
-
我建议你使用
FormControl作为你的用户输入(如果它是一个输入元素)并订阅它,在订阅中然后调用你的this.servicio.goWiki函数并订阅它,你的userInput属性是null或undefined并且服务可能返回404或500。 -
@RezaRahmati this error 出现。
-
这只是一个 CORS 错误。使用代理服务器绕过该限制
标签: angular typescript httpclient mediawiki ionic4