MissingServletRequestParameterException:必需的长参数prod_id不存在

时间:2018-09-30 09:40:08

标签: ajax spring spring-boot

当我向Postman发出发帖请求时,它工作正常,但是从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);
    }

Ajax请求

$(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");
            }
            });

    });

1 个答案:

答案 0 :(得分:0)

在删除contentType:“ application / json; charset = utf-8”后,问题已解决,因为@Postmapping仅接受“ application / x-www-form-urlencoded”。因此,如果我定义contentType,则可以正常工作