Resolver problemas Ionic5 / Angular8 utilizados en el proyecto angular del HttpClient.post petición no válida
Soluciones directamente en la
interfaz de servidor Java escrita 1
@Description("获取计划执行监控总体数据列表")
@POST
@Path("/getPlanExecMonitorTotalData")
@Produces(MediaType.APPLICATION_JSON)
public String getPlanExecMonitorTotalData(
@FormParam("dsType") @DefaultValue("demoDs") String dsType,
@FormParam("planDate") String planDate,
@FormParam("equipGroupId") String equipGroupId,
@FormParam("equipJson") String equipJson) {
}
puntos de interfaz Java Server posterior solicitud
Clave 1: anotaciones add @Produces (MediaType.APPLICATION_JSON) en los métodos de interfaz.
Key 2: annotate parámetros del método @FormParam.
2, la redacción extremo delantero
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { EnvService } from '../../wongoing/env.service';
import { UtilsService } from '../../wongoing/utils.service';
@Injectable({
providedIn: 'root'
})
export class JihuaService {
constructor(private http: HttpClient, private env: EnvService, private utilsService: UtilsService) { }
/**
* 获取计划执行监控总体数据列表
* @param planDate 计划日期
* @param equipGroupId 生产区域ID
* @param equipJson 机台列表json串[{equipCode:'', equipName:''}, {equipCode:'', equipName:''}]
*/
public getPlanExecMonitorTotalData(planDate, equipGroupId, equipJson) {
const url = this.env.getPlanExecMonitorTotalData;
const method = 'POST';
const dsType = this.utilsService.getDataSourceType();
const usedLanguage = this.utilsService.getUsedLanguage();
let body = new HttpParams();
body = body.append('dsType', dsType);
body = body.append('planDate', planDate);
body = body.append('equipGroupId', equipGroupId);
body = body.append('equipJson', equipJson);
const options = { headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded') };
// const options = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }; // 效果等同上一句
return this.http.post(url, body.toString(), options).toPromise();
}
}
Key 1: El segundo formato de parámetro this.http.post cuerpo debe ser param1 = valor1 y param2 = valor2 este formato, el método tal como se usa en el presente documento el toString () HttpParams objeto en los parámetros clave tales formato de cadena.
Clave 2: El tercer parámetro para ajustar this.http.post encabezado de solicitud Content-Type es application / x-www-urlencoded.
3, el cartero en la interfaz de prueba
Nota: es la solicitud del ajuste de parámetros urlencoded-x-www-form cuerpo, en lugar de los parámetros establecidos form-data.