контроллер: < /p>
Код: Выделить всё
@PostMapping("/deleteMultipleMyObjects")
@ResponseBody
public ResponseEntity deleteMultipleMyObjects(Model model, @ModelAttribute("myObjectIds") String myObjectIds) {
if (myObjectIds.length() == 0) {
MessageHelper.addInfoAttribute(model, "objects.delete.none");
return ResponseEntity.badRequest().body("fragments/components :: ajaxResponse");
}
// The service brings back two lists : the ids deleted and those non-deleted (for any reason)
Pair res = myObjectService.deleteMultipleMyObjects(myObjectIds);
if (!res.getValue1().isEmpty()) {
// Here I want to send back a warning, and handle the objects I did not delete
MessageHelper.addWarningAttribute(model, "objects.delete.warn", res.getValue1());
return ResponseEntity.status(HttpStatus.PRECONDITION_FAILED).body(new Pair(res.getValue0(), "fragments/components :: ajaxResponse"));
}
// If all goes well, success
MessageHelper.addSuccessAttribute(model, "objects.delete.ok");
return ResponseEntity.accepted().body("fragments/components :: ajaxResponse");
}
< /code>
фрагмент, который я хочу получить (и который используется много раз в моем коде) - components.html: < /p>
×
An error happened
An error happened
< /code>
html, где я визуализирую и обрабатываю свои объекты-objects.html: < /p>
Delete multiple objects
function deleteMultipleMyObjects() {
let myObjectIds = getAllCheckedObjects(); // sends back a string with the ids of the checked objects : "id1,id3,id6"
let fragment;
$.post("deleteMultipleMyObjects", {myObjectIds: myObjectIds})
.done(function (data, status, xhr){
hideAllCheckedObjects();
fragment = data; // !!! Here I get the name of the fragment instead of the html
})
.fail(function(xhr, status, error){
if (undefined != xhr.responseJSON && xhr.responseJSON.value0.length > 0) {
hideLinesWithId(xhr.responseJSON.value0);
fragment = xhr.responseJSON.value1; // !!! Here I get the name of the fragment instead of the html
} else {
fragment = xhr.responseText; // !!! Here I get the name of the fragment instead of the html
}
})
.always(function(xhr, status, error) {
$("#alertDiv").text("");
$("#alertDiv").append(fragment); // !!! Here I'd like my alertDiv to receive the full html of my fragment
});
}
< /code>
Каждый раз, когда я получаю XHR или данные, содержащие буквально название фрагмента («фрагменты /компоненты :: ajaxResponse») вместо HTML.
У меня есть другие места в моем коде, где я использую $ .ajax или $ .post, и я возвращаю HTML фрагмента, но в этих местах мой контроллер отправляет обратно строку, а не ответ. Здесь я бы очень хотел, чтобы мой код ответа HTTP для обработки различных ответов, а также тело с данными, а также с моим фрагментом. Кажется, я не могу их всех ...
Подробнее здесь: https://stackoverflow.com/questions/794 ... -ajax-post
Мобильная версия