使用Angular的http client发送请求,请求response总是被当成json类型处理
生活随笔
收集整理的這篇文章主要介紹了
使用Angular的http client发送请求,请求response总是被当成json类型处理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
奇怪的問題,我的req.responseType字段沒有顯式賦值,而默認值為json:
這個默認值是在哪里填充的呢?
調(diào)試代碼,http.js的第1669行有這個默認的邏輯:
解決方案:下圖第26行
responseType: 'text' as 'json' import { Component, OnInit } from '@angular/core'; import { FormControl } from '@angular/forms'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable } from 'rxjs';const ENDPOINT = "http://localhost:3000/echo?data=";@Component({selector: 'jerryform',template: `<input type="text" [formControl]="favoriteColorControl">` }) export class ReactFormComponent implements OnInit {constructor(private http:HttpClient){}ngOnInit(): void {const headers: HttpHeaders = new HttpHeaders({'Accept': 'text/html',});const options = {headers: headers,responseType: 'text' as 'json'}this.favoriteColorControl.valueChanges.subscribe((value) =>{console.log('new value: ' + value);var $http = this.http.get('http://localhost:3000/angular/' + value, options);$http.subscribe((response)=>console.log('response from http: ' + response),(error)=>console.log('error: ' + error));})}favoriteColorControl = new FormControl(''); }參考issue
更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:
總結(jié)
以上是生活随笔為你收集整理的使用Angular的http client发送请求,请求response总是被当成json类型处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gta5线上拉玛任务
- 下一篇: 华为手机计算器自动旋转怎么关闭