【发布时间】:2018-04-19 09:22:16
【问题描述】:
我发出了一个 POST 请求,从我的项目后端向数据库添加一个新实体 我在邮递员上测试过,效果很好
但是当我尝试从前端 UI 添加一个新实体时(我使用的是 Angular 4),我收到了这个错误:缺少所需的请求正文:
加载资源失败:服务器响应状态为 500 (Erreur Interne de Servlet)
我猜我的打字稿代码有问题 谁能帮我解决这个问题?
<form (ngSubmit)="createEntity()">
<div class="form-group">
<label for="code">Code</label>
<input type="text" [(ngModel)]="code" placeholder="Code" name="code" class="form-control" id="code">
</div>
<div class="form-group">
<label for="description">Description</label>
<input [(ngModel)]="description" placeholder="Description" name="description" class="form-control" id="description">
</div>
<div class="form-group">
<label for="type">Type</label>
<p-dropdown [(ngModel)]="type" [options]="entityTypes" filter="filter" name="type" placeholder="Type" [style]="{'width':'100%'}">
</p-dropdown>
</div>
<div class="form-group">
<label for="numero">Profil</label>
<p-dropdown [(ngModel)]="profil" [options]="entityProfiles" filter="filter" name="profil" placeholder="Profil" [style]="{'width':'100%'}">
</p-dropdown>
</div>
<div class="form-group">
<label for="number">number</label>
<input [(ngModel)]="number" placeholder="number" name="number" class="form-control" id="number">
</div>
<button type="submit" class="btn btn-success">Valider</button>
</form>
entity: Entity;
export interface Entity {
id?: string;
code?: string;
description?: string;
type?: string;
number?: string;
profil?: string;
}
createEntity(): void {
this.EntitySRV.CreateEntity(this.entity)
.subscribe(data => {
alert("Entity created successfully.");
});
};
// entity service .ts
CreateEntity(entity) {
const entityUrl = this._entity;
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Headers', 'Content-Type, Accept');
return this.http.post(entityUrl, entity, { headers })
.map(res => {
return res.json();
})
.catch(this.handleError);
}
//RestController :
@PostMapping
public Entite create(@RequestBody Entite entite){
return entityService.create(entite);
}
//Service
@Override
public Entite create(Entite entite) {
entityDao.persist(entite);
return entite;
}
// DAO
private static final String QUERY_SAVE_ENTITY = " INSERT INTO Entite(code, description, numero, profil,type)";
@Override
public Entite saveEntity() {
return (Entite) executeQuery(QUERY_SAVE_ENTITY);
}
提前谢谢你:)
【问题讨论】:
-
您的 http 标头在哪里?
-
我删除了,需要吗?
-
我添加了标题但它返回相同的错误: CreateEntity(entity) { const entityUrl = this._entity;常量标头 = 新标头(); headers.append('Content-Type', 'application/json'); headers.append('Access-Control-Allow-Headers', 'Content-Type, Accept'); return this.http.post(entityUrl, entity, { headers }) .map(res => { return res.json(); }) .catch(this.handleError); }
-
加载资源失败:服务器响应状态为 500 (Erreur Interne de Servlet)
-
请问你能不能给出你的具体代码,这样我调试起来就方便了
标签: angular typescript spring-mvc angular4-forms