当我向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");
}
});
});
答案 0 :(得分:0)
在删除contentType:“ application / json; charset = utf-8”后,问题已解决,因为@Postmapping仅接受“ application / x-www-form-urlencoded”。因此,如果我定义contentType,则可以正常工作>