我是编程的初学者,我想要一些帮助。 我想将数据从数据列表发送到我的其余服务器。所选项目和ID将发送到我在glassfish 5.191上运行的Restful服务器。我收到错误405方法不允许。我究竟做错了什么?我希望有一个人可以帮助我。我的前端是用Angular制造的。
我重新部署了服务器并检查了异常情况。
前端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>
前端ts文件:
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;
this.http.post('http://localhost:8080/aquadine-jee/resources/restaurant', {
id: this.restaurant.id,
name: this.restaurant.name
})
.subscribe( // subscribe to observable http.post
res => {
console.log(res); // log results otherwise log error
},
err => {
console.log('Error occured');
}
);
后端:
public class Restaurant {
@Id
private Integer id;
/
private Integer restaurantId;
private String naam;
// @Column
// @NotBlank
// private String adres;
public Restaurant(){
}
public Restaurant(Integer id, Integer restaurantId , String naam) {
this.id = id;
this.restaurantId = restaurantId;
this.naam = naam;
}
发布方法:
@POST
@Path("restaurant")
@Consumes("application/json")
public Response save(Restaurant restaurant){
repositoryService.save(restaurant);
return Response
.status(201)
.build();
}
@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
@Path("restaurant")
@Consumes("application/json")
public Response save(Restaurant restaurant){
repositoryService.save(restaurant);
return Response
.status(201)
.build();
}
@PUT
@Consumes("application/json")
public void update(Restaurant restaurant) {
repositoryService.update(restaurant);
}
@DELETE
@Path("/{id}")
@Consumes("application/json")
public void delete(@PathParam("id") Integer id) {
Restaurant restaurant = repositoryService.find(id);
repositoryService.delete(restaurant);
}
}