首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >触发了使用Angular HTTP客户端的HTTP删除发送-未被服务器接收

触发了使用Angular HTTP客户端的HTTP删除发送-未被服务器接收
EN

Stack Overflow用户
提问于 2020-03-25 04:26:40
回答 1查看 64关注 0票数 0

我正在开发一个包括CRUD功能的应用程序。我使用Spring作为后端实现,Angular作为前端实现。在前端,我像这样使用Angular的http客户端来删除一个实体(为了简洁起见,以下代码被缩短了):

组件:

代码语言:javascript
复制
export class RecipeComponent implements OnInit {

  constructor(private recipeService : RecipeService) { }

    deleteElement (id : number) {
        this.recipeService.deleteRecipe(id).subscribe();
    }
}

服务:

代码语言:javascript
复制
export class RecipeService {

    deleteRecipe (id : number) : Observable<{}> {
      return this.http.delete('http://localhost:8080/recipe/' + id);
    }

  constructor( private http: HttpClient ) { }
}

在执行函数时,我可以在network选项卡中找到http-delete请求:

Spring控制器看起来(简称)如下:

代码语言:javascript
复制
@ControllerAdvice
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping(path="/recipe")
public class RESTController {
    @Autowired
    private RecipeRepository recipeRepository;


@DeleteMapping(path="/{id}")
public @ResponseBody ResponseEntity deleteRecipe (@RequestBody Recipe deletedRecipe, @PathVariable("id") int id) {

    try {
        [...]
        }
    } catch (RecipeNotFoundException e) {
        [...]       
    }
}


}

然而,我的后端没有收到请求。奇怪的是,发布或获取工作都很好。

谁能给我指个方向?

谢谢你,祝你好运!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-25 05:13:12

400 Bad request很可能是"body is missing“错误。从方法参数中删除@RequestBody Recipe deletedRecipe由于您没有提供要删除的实体,因此id就足够了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60838773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档