import { BrowserModule } from '@angular/platform-browser'; import { HttpModule } from '@angular/http'; imports: [ BrowserModule, HttpModule ],
import { Http , ResponseOptions , Headers , URLSearchParams } from '@angular/http';
constructor( private http: Http ) { }
①:帶參數的post(必定要使用URLSearchParams進行封裝)json
getData() { const d = new URLSearchParams(); d.append('para', 'value' ); d.append('para1', 'value' ); this.http.post( '地址' , d) .map(res => res.json()) // (5) .subscribe(data => { alert(JSON.stringify(data)); }, err => { console.error('ERROR', err); });
②:帶參數的get請求app
getData() { const dates = { 'str': 123 }; this.http.get('地址' , {params: dates}) .map(res => res.json()) .subscribe(data => { alert(JSON.stringify(data)); }, err => { console.error('ERROR', err); });
③:不帶參數的get請求函數
getData() { this.http.get('/hello') .map(res => res.json()) .subscribe(data => { alert(JSON.stringify(data)); }, err => { console.error('ERROR', err); });
@RequestMapping("/hello") @ResponseBody public String hello(HttpServletRequest req) { System.out.print(req.getParameter("str")); Gson gson = new Gson(); String s1= gson.toJson(1); return s1; }