【发布时间】:2019-05-21 11:25:57
【问题描述】:
我正在尝试将两个数据发送到我在 glassfish 上运行的 RESTful 服务器。用户必须在数据列表中选择一家餐厅。我想发送餐厅的 ID 和名称。使用 console.log 我可以打印出 id 和 name 的值,但我的响应返回 null。知道为什么会发生这种情况以及如何解决吗?
ts 文件
export class OrganizeComponent implements OnInit {
public codeValue: string;
codeList = [
{ id: 1, name: 'Mcdonalds' },
{ id: 2, name: 'Kentucky Fried Chicken' },
{ id: 3, name: 'Burger King' },
{ id: 4, name: 'Domino`s pizza' },
{ id: 5, name: 'New York Pizza' }
];
@ViewChild('f') form: NgForm;
restaurant = {
id: 0,
name: ''
};
constructor(private http: HttpClient) {
}
ngOnInit() {
}
public saveCode(e): void {
let name = e.target.value;
let list = this.codeList.filter(x => x.name === name)[0];
// console.log(list.id);
// console.log(list.name)
this.restaurant.id = list.id;
this.restaurant.name = list.name;
console.log(list.id);
console.log(list.name);
this.http.post('http://localhost:8080/aquadine-jee/resources/restaurant', {
id: list.id,
name: list.name
})
.subscribe( // subscribe to observable http.post
res => {
console.log("response" + " " + res); // log results otherwise log error
},
err => {
console.log('Error occured');
}
);
HTML 文件
<label for="codes">Choose a restaurant:</label>
<form
(ngSubmit)="onOrganize(f)"
#f="ngForm">
<input type="text" list="codes" [(ngModel)]=codeValue [ngModelOptions]="{standalone: true}" (change)="saveCode($event)">
<datalist id="codes">
<option *ngFor="let c of codeList" [value]="c.name" >{{c.name}}</option>
</datalist>
</form>
后端代码:
@Stateless
@Path("restaurant")
public class RestaurantResource {
@Inject
private RepositoryService repositoryService;
@GET
@Path("restaurant")
@Produces("application/json")
public Response all(){
List<Restaurant> all = repositoryService.getAllRestaurants();
return Response
.status(200)
.header("Access-Control-Allow-Origin", "*")
.entity(all)
.build();
}
@GET
@Produces("application/json")
public List<Restaurant> getAllRestaurants(){
return repositoryService.getAllRestaurants();
}
@POST
@Consumes("application/json")
public Response save(Restaurant restaurant){
repositoryService.save(restaurant);
return Response
.status(201)
.build();
}
public class Restaurant {
@Id
@Column(name="idRestaurant")
@NotBlank
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
public Restaurant(Integer id,Integer restaurantId , String naam) {
this.id = id;
this.restaurantId = restaurantId;
this.naam = naam;
}
后端服务出错:
内部服务器错误 500。 在 EJB RepositoryService 上调用,方法:public void nl.aquadine.service.RepositoryService.save(nl.aquadine.model.Restaurant)]] 和 javax.ejb.TransactionRolledbackLocalException:抛出异常 来自 bean 和引起:org.hibernate.PersistentObjectException: 分离实体传递给坚持:nl.aquadine.model.Restaurant at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124) 在 org.hibernate.event.internal.DefaultPersis 等处。
【问题讨论】:
-
如果响应为空,您必须在后端检查那里发生了什么。您必须调试后端,而不是前端(Angular 应用程序)。
-
如果您的目标是通过 JSON 发送,那么您缺少正确的标头
-
@jcuypers Angular Http 客户端设置 json 帖子的标题。这是这个库的默认行为。更多信息:angular.io/guide/http#making-a-post-request
-
我添加了一些后端代码。我似乎找不到错误
-
@ChristianBenseler 我看不出在哪里可以找到默认行为。它没有写在任何地方,示例还指定了标题。英雄示例定义了一些基本的标头,每次调用都包含这些标头。这不是默认行为
标签: angular rest http glassfish