所以,我有我的spring boot控制器和ajax函数来获取响应数据,在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);
});
}
});
Librery for spring boot将pom.xml中的对象转换为JSON格式
<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>
我试图从数据库中删除报价,我从我的页面发送了请求,报价也从数据库中删除了,但是响应不太好,当我发送新的响应(msg,responseHeaders,HttpStatus.not_FOUND)时,我仍然在ajax函数中获得未定义的数据。
我想在ajax中获取JSON格式的Offer对象,以便从Offer对象访问id。