首页 > 解决方案 > MissingServletRequestParameterException:必需的长参数 prod_id 不存在

问题描述

当我从邮递员发出帖子请求时,它工作正常,但从 ajax 发出以下错误。请帮助。提前致谢

服务器端

@PostMapping(value = "/delete")
    public ResponseEntity<BaseResponse> delete(@RequestParam("prod_id") Long productId) throws URISyntaxException {
        BaseResponse response = new BaseResponse();

        try{
            productRepository.deleteById(productId);
            response.setStatus(MessageType.SUCCESS);

        }catch (Exception e){
            response.setStatus(MessageType.FAIL);
        }
        return ResponseEntity.ok(response);
    }

阿贾克斯请求

$(document).on('click','.delIcon',function(){
        var row1 = $(this).closest('tr');
        row = row1;
        var data = $('#datatable').dataTable().fnGetData(row1);
        var productId = data[0];

            $.ajax({
            url: "delete",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: {"prod_id": productId  },
            success: function(response) {
                if(response.status == 'SUCCESS' ){
                      alert('Deleted Successfully');
                }
            },
            error: function(xhr) {
                alert("Delete response got");
            }
            });

    });

标签: ajaxspringspring-boot

解决方案


删除 contentType: "application/json; charset=utf-8" as @Postmapping 后问题已解决


推荐阅读