所以,在我的ajax成功获得响应之后,我有了我的spring-boot控制器和ajax函数来获取响应数据,但是该数据未定义,但是HTTP状态为204,这很好,但是为什么要使用该数据是未定义的,以及如何在我的ajax中获取它以访问要获取的对象中的属性?
@DeleteMapping(value = "/**/delete/{id}")
public ResponseEntity<?> delete(@PathVariable Integer id) {
Optional<Offer> optional = this.offerRepository.findById(id);
if (optional.isPresent()) {
this.offerRepository.deleteById(id);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
Offer offer = optional.get();
return new ResponseEntity<Offer>(offer, responseHeaders, HttpStatus.NO_CONTENT);
} else {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.TEXT_HTML);
String msg = "Offer is not found";
return new ResponseEntity<String>(msg, responseHeaders, HttpStatus.NOT_FOUND);
}
}
$.ajax({
url: "/" + currentUserRoleName + '/delete' + "/" + offerIdValue,
scriptCharset: 'UTF-8',
method: 'DELETE',
success: function (data, textStatus, jqXHR) {
console.log(data); // get undefine
console.log(jqXHR.status); //status is 204
var offer = $.parseJSON(data.responseText); //here error call of undefine
if (jqXHR.status === 204 || jqXHR.status === 200) {
// var id = offer.id;
var selector = "input#offerHiddenId[value = '" + offerIdValue + " ']";
var hiddenInputWithOfferId = $(".main_out_wrapper_user_offers").css(selector);
var parent = hiddenInputWithOfferId.closest("div.inner_main_out_wrapper_user_offers");
parent.empty();
$("#no").click();
} else {
alert("Can't delete the offer");
}
},
error: function (data) {
alert(data.responseText);
var div = $.createElement("div");
var span = $.createElement("span");
span.innerHTML = "Offer is not found";
div.appendChild(span);
div.fadeIn(1000, function () {
div.fadeOut(1000);
});
}
});
<properties>
<jaction.xml.version>2.9.6</jaction.xml.version>
</properties>
<!--FOR JSON-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jaction.xml.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jaction.xml.version}</version>
</dependency>
我尝试从数据库中删除商品,然后从页面发送请求,商品从数据库中删除,但是当我发送新的ResponseEntity(msg,responseHeaders,HttpStatus时,响应不是很好。 NOT_FOUND)我的ajax函数中仍然得到未定义的数据。
我想以ajax格式获取JSON格式的Offer对象,以便从offer对象访问ID。