实际上我已经连接到web api和获取数据,在所有示例中他们都用本地数据解释并且我对这些导入语句感到困惑,请任何人都能清楚解释..谢谢
答案 0 :(得分:0)
您需要使用HTTP模块。在您订阅(给它'热' /活动)之前,http请求不会触发
这是一个基本的例子:
import { Component, ViewEncapsulation } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'test',
template: '<div></div>'
})
export class TestComponent {
private data;
private error;
constructor(private http: Http) {
callWebApi();
}
callWebApi()
{
let url ='http://www.test.com/api/xyz';
let body = {
'Param1': 1234,
'Param2': 5678
};
this.http.post(url, body)
.subscribe(
data => {
let rJson = data.json();
// process json here
alert('success');
},
error => {
// handle error
alert('error');
});
}
}