//module.ts 文件
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, HttpClientModule, HttpClientJsonpModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
//component.ts 文件
import {HttpClient,HttpClientJsonpModule } from "@angular/common/http"; //导入http模块,HttpClientJsonpModule 为JSONP请求导入的模块
constructor(public http:Httpclient) {} //构造函数中声明
const url= "http://XXXX";
//get请求
this.http.get(url).subscribe (response =>{
console.log(response);
})
//get 获取完整请求体 { observe: 'response' }
this.http.get(url,{ observe: 'response' }).subscribe (response =>{
console.log(response);
})
//post请求
const httpOptions = { //请求头
headers: new HttpHeaders({
'Content-Type': 'application/json',
Authorization: 'my-auth-token'
})
};
let params = {"age":22,"username":"
angular-http发送请求
最新推荐文章于 2022-05-31 22:39:50 发布