angular中的http

angular中的http:

参考:
官网:https://angular.cn/api/common/http/HttpClient#get
个人:
https://blog.csdn.net/It_rod/article/details/79811009
https://blog.csdn.net/u013451157/article/details/79519719

  • get请求
// 请求参数:
// headers:请求头;observe:res里有响应头;params:get请求的body参数;

const options = {
  headers: new HttpHeaders({
    'currentApp': 'currentApp',
    'currentCode': 'currentCode'
  }),
  observe: 'response',
  params: params,
};

this.http.get('/serviceName/api/user/get-user-info', options).subscribe(res => {
  console.log('获取用户信息接口:', res);
  console.log('body:', res.body); // 请求的响应体body
  console.log(res.headers.get('status_code')); // 获取响应头里面的status_code值
});
  • post请求
    // params单独第二个参数使用:
const options = {
  headers: new HttpHeaders({
    'currentApp': 'currentApp-login',
    'currentCode': 'currentCode-login'
  }),
  observe: 'response',
  // params: params,
};
this.http.post('/auth/login', params, options).subscribe(res => {
  console.log('登录接口:', res);
});

注:如果params放在options里面,那么参数会自动拼写在接口后面;eg:
/auth/login?username=uSqyRKsMrkpg0X5tG7CQXg==&password=gBV3H+S/xW0=
即:在接口后面拼写分页的方法。

ts语法检查错误:

 Argument of type
  '{ 'observe': string; 'params': {}; 'headers': HttpHeaders; }' 
  is not assignable to parameter of type 
 '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
 
  Types of property 'observe' are incompatible.
  Type 'string' is not assignable to type '"body"'.

==>这个错误在编译的时候会报错;没有找到好的解决办法;
so:我把参数放在函数内部,不提出来就ok了,eg:

// options不提出来,就是直接放在函数 内。
this.http.get('/serviceName/api/user/get-user-info', {
  headers: new HttpHeaders({
    'currentApp': 'currentApp',
    'currentCode': 'currentCode'
  }),
  observe: 'response',
  params: params,
}).subscribe(res => {
  console.log('获取用户信息接口:', res);
  console.log('body:', res.body); // 请求的响应体body
  console.log(res.headers.get('status_code')); // 获取响应头里面的status_code值
});

继续把http放入base-service中,请参考:
https://blog.csdn.net/weixin_42995876/article/details/95352550

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular HTTPAngular框架的一个模块,用于处理HTTP请求和响应。它提供了一组功能强大的API,可以方便地进行HTTP通信。在Angular,可以使用HttpClient模块来发送HTTP请求,并使用Observables来处理响应。 要在Angular使用HTTP模块,首先需要在应用的根模块或特定模块导入HttpClientModule。例如,在app.module.ts文件,可以使用以下代码导入HttpClientModule: import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { HttpClientModule } from '@angular/common/http'; @NgModule({ imports: [ BrowserModule, HttpClientModule, // ... 其他模块 ... ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule {} 一旦导入了HttpClientModule,就可以在组件注入HttpClient服务,并使用它来发送HTTP请求。例如,可以在组件注入HttpClient,并使用get()方法发送一个简单的GET请求: import { HttpClient } from '@angular/common/http'; @Component({ // 省略组件的其他配置 }) export class MyComponent { constructor(private http: HttpClient) {} getData() { this.http.get('http://example.com/api/data').subscribe((response) => { // 处理响应数据 }); } } 以上示例代码演示了如何使用Angular HTTP模块发送一个简单的GET请求。根据实际需求,还可以使用HttpClient模块发送POST、PUT、DELETE等类型的请求,并使用不同的参数和选项来定制请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值