我将 Content-Type 标头设置为application/json
fromfetchTokenUsingPasswordFlow
方法,但它是application/x-www-form-urlencoded
. 有没有办法将标题内容类型设置为application/json
?
根据源代码,Content-Type
标头已被硬编码为application/x-www-form-urlencoded
.
我正在为后端使用 Spring Boot Rest 服务,但它不允许application/x-www-form-urlencoded
作为Content-Type
. 请在下面找到示例 Angular 6 代码供您参考:
import { Component, OnInit, Input } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Usermodel } from '../models/usermodel';
import { OAuthService } from 'angular-oauth2-oidc';
import { HttpHeaders } from '@angular/common/http';
@component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
@input() message: any;
@input() apiUrl: any;
@input() params: any;
currentUser: Usermodel;
model: any = {};
loading = false;
returnUrl: string;
headers: HttpHeaders;
constructor(private router: Router,
private route: ActivatedRoute,
private oauthService: OAuthService,
) {
oauthService.tokenEndpoint = "http://localhost:7890/api/login";
oauthService.requireHttps = false;
this.oauthService.setStorage(localStorage);
this.headers = new HttpHeaders().set('Content-Type', 'application/json');
console.log('oauthtoken', this.oauthService.getAccessToken());
}
ngOnInit() {
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
}
public login() {
this.loading = true;
this.apiUrl = 'login'
console.log("Headers::->" + this.headers)
this.oauthService.fetchTokenUsingPasswordFlow(this.model.userName, this.model.password, this.headers).then((resp) => {
console.log('resp', resp);
});
}
}