Angular2 - HTTP Request Options 헤더
저는 현재 tslint에 문제가 있으며 누군가가 올바른 방향으로 나를 가리킬 수 있기를 희망하고 있었습니다.
Angular2 프레임워크에서 제공하는 HTTP를 이용하여 HTTP GET 요청을 보내려고 합니다.이 요청으로 컨텐츠 타입과 베어러 인증 토큰을 지정해야 합니다.
내 코드의 예:
let headers = new Headers();
let authToken = this._user.getUser().JWT;
headers.append('Content-Type', 'application/json');
headers.append('Authorization', `Bearer ${authToken}`);
let options = new RequestOptions({ headers: headers });
this._http.get('http://' + url '/', options)
.timeout(3000)
.subscribe(
(res) => {
이것은 효과가 있지만, tslint는 다음과 같이 불평하고 있습니다.
"TS2345: '{ 헤더 유형 인수:헤더; }'은(는) 'RequestOptions' 유형의 매개 변수에 할당할 수 없습니다.Args. '헤더' 속성 유형이 호환되지 않습니다.'헤더' 유형은 '헤더' 유형에 할당할 수 없습니다.이 이름을 가진 두 가지 유형이 있지만 서로 관련이 없습니다.속성 '키'가 '헤더' 유형에 없습니다."
응원해주셔서 감사합니다.
갱신하다
오늘부로.@angular/http
감가 상각되었습니다.@angular/common/http
대신 사용해야 합니다.따라서 http 헤더를 사용하는 가장 좋은 방법은 가져오기입니다.import { HttpHeaders } from '@angular/common/http';
(document).
구답
그Headers
당신이 가져오기로 되어있는 타입은import { Headers } from '@angular/http';
.
가져오기 확인
다음 기준으로 헤더를 업데이트해야 합니다.
let headers = {headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded'})};
각도 5에 대한 업데이트
import { RequestOptions } from '@angular/http';
제가 정답 댓글에서 발견한 거라서 이게 도움이 된다면 행운을 빌어요.
문서: https://angular.io/api/http/RequestOptions
// 내용 유형 Json의 헤더 예제
import { Http, Headers, RequestOptions } from '@angular/http';
const Url = 'http://localhost:3000/';
const headers = new Headers;
const body = JSON.stringify({
title: "data"
});
headers.append('Content-Type', 'application/json');
this.http.post(Url, body, { headers: headers })
.pipe(map((res => res)));
언급URL : https://stackoverflow.com/questions/43205570/angular2-http-requestoptions-headers
'programing' 카테고리의 다른 글
Oracle Alter 명령을 사용하여 기존 Column 오류 링의 이름을 변경합니다. (0) | 2023.10.29 |
---|---|
리눅스에서 GetTickCount()와 동등함 (0) | 2023.10.29 |
.js 파일 대 CDN 번들링 (0) | 2023.10.29 |
변경 시 선택한 값/텍스트 가져오기 (0) | 2023.10.29 |
서로 다른 두 사용자 테이블 간에 워드프레스 사이트 공유 (0) | 2023.10.29 |