先推荐大神的教程https://www.joshmorony.com/using-http-to-fetch-remote-data-from-a-server-in-ionic-2/
前端小白的我找寻许久的方式
首先在app.module.ts引入
import { HttpModule } from '@angular/http';
@NgModule({
imports: [
HttpModule
]
})
然后页面引入
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
export class HomePage {
constructor( public http: Http) { ...}
let url="http:www.baidu.com"
//get请求
this.http.get(url).map(res => res.json()).subscribe(data => {
console.log(data);
});
//post请求
let data=JSON.stringify({
user:"admin"
})
this.http.post(url, data).map(res => res.json()).subscribe(data => {
console.log(data);
}); //.map(res => res.json())相当于把json字符串转化成json对象。
网友评论