我已将代码从Angular 2升级到了Angular6。http标头中设置的参数为content-type
和authorization
。 authorization
包含btoa
的用户名和密码。
我找不到设置httpheaders参数的错误。
这是从Spring启动授权的。附加了appmodule
和service
的代码。
应用模块
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule,HttpHeaders } from '@angular/common/http';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MenuComponent } from './components/menu/menu.component';
import { LoginComponent } from './components/login/login.component';
import { AngularMaterialModule } from './angular-material.module';
import { AppRoutingModule } from './/app-routing.module';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
MenuComponent,
LoginComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
HttpClientModule,
AngularMaterialModule,
AppRoutingModule
],
providers: [HttpHeaders],
bootstrap: [AppComponent]
})
export class AppModule { }
登录服务
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders} from '@angular/common/http';
import { Token } from '../models/Token';
@Injectable({
providedIn: 'root'
})
export class LoginService {
constructor(private http: HttpClient,private header: HttpHeaders) { }
sendCredential(username: string, password: string) {
let url = 'http://localhost:8080/token';
let headers = new HttpHeaders({
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization' : 'Basic ' + btoa(username+":"+password)
});
return this.http.get<Token>(url, { headers });
}
checkSession() {
let url = 'http://localhost:8080/checkSession';
let headers = new HttpHeaders({
'x-auth-token' : localStorage.getItem('xAuthToken')
});
return this.http.get(url, { headers });
}
logout() {}
}
答案 0 :(得分:1)
在登录服务中
constructor(private http: HttpClient) { }
答案 1 :(得分:0)
感谢您的答复。
通过指定以下语句解决了该问题:app模块中的HttpHeaders。
声明是
提供者:[{provide:HttpHeaders}]