CKEditor5 μ΄λ―Έμ§ μ λ‘λ μ JSON μλ΅ μ€λ₯ ν΄κ²°
β μ€λ₯ μν©
CKEditor5μμ μ΄λ―Έμ§λ₯Ό μ λ‘λνλ €κ³ ν λ λ€μκ³Ό κ°μ μ€λ₯κ° λ°μ
SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON
λλ ν¬λ‘¬ μ½μ μ€λ₯
POST http://localhost:8080/editor/uploadImage 200 (OK)
Unexpected token '<'
λΈλΌμ°μ μλ μ΄λ―Έμ§κ° κΉ¨μ§ μνλ‘ λνλκ³ , μ½μμλ JSON νμ± μ€ν¨ μ€λ₯κ° λ°μ
π― μμΈ
CKEditor5λ μ΄λ―Έμ§ μ λ‘λ μ fetchλ₯Ό ν΅ν΄ μλ²μ μ΄λ―Έμ§λ₯Ό μ μ‘, λ°λμ μλμ κ°μ JSON νμμ μλ΅μ κΈ°λ
{
"url": "/editor/image?name=νμΌλͺ
.png"
}
μλ²μμ λ€μκ³Ό κ°μ μλͺ»λ μλ΅μ λ°ννλ©΄ μ€λ₯ λ°μ
• HTML λ¬Έμ μ 체 (μ: JSP νμ΄μ§)
• λ¬Έμμ΄ "error" λλ "false" (JSON νμμ΄ μλ)
• λΉ λ¬Έμμ΄
• JSON νμμ²λΌ 보μ΄μ§λ§ "Content-Type"μ΄ text/htmlλ‘ λμ΄ μμ
μ¦, JSONμ΄ μλ μλ΅μ λ°μμ CKEditorμμ νμ±νλ€κ° μ€λ₯κ° λλ κ²
β ν΄κ²° λ°©λ²
β μλ² (Spring Controller)μμ μλ΅μ λ°λμ JSON νμμΌλ‘ μ€μ
@PostMapping("/editor/uploadImage")
@ResponseBody
public ResponseEntity<Map<String, Object>> uploadImage(@RequestParam("upload") MultipartFile file) {
try {
String imageUrl = "/editor/image?name=" + μ μ₯λνμΌλͺ
;
return ResponseEntity.ok(Map.of("url", imageUrl)); // JSON μλ΅
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(Map.of("error", "μ΄λ―Έμ§ μ
λ‘λ μ€ν¨"));
}
}
• λ°λμ Map.of(...) λλ DTO μ¬μ©
• @ResponseBody λλ @RestController νμ
• μλ΅ Content-Typeμ μλμΌλ‘ application/jsonμ΄ μ€μ λ¨
β ν΄λΌμ΄μΈνΈ (JS)μμ JSON μλ΅λ§ μ²λ¦¬
fetch('/editor/uploadImage', {
method: 'POST',
body: formData
})
.then(response => response.json()) // JSON μλ΅λ§ λ°μμΌ ν¨
.then(data => {
if (data.url) {
resolve({ default: data.url });
} else {
printSweetAlert("error", "μ΄λ―Έμ§ μ
λ‘λ μ€ν¨", "λ€μ μλν΄μ£ΌμΈμ.");
reject("μλ² μλ¬ μλ΅");
}
})
.catch(error => {
printSweetAlert("error", "ν΅μ μ€λ₯", "μλ²μμ μ°κ²°μ λ¬Έμ κ° μμ΅λλ€.");
reject(error);
});
• HTMLμ΄ λ°νλ κ²½μ° response.json() λ¨κ³μμ μλ¬ λ°μν¨
• resolve() λλ reject() νΈμΆμ λ°λΌ μλν° UIμ λ°λ‘ λ°μλ¨
π λΆκ° ν
1. JSP λ΄μμ <script>window.alert = function(){};</script> μ€μ μΌλ‘ κΈ°λ³Έ alert μ°¨λ¨ → SweetAlert μ¬μ©
2. editor.js, updateBoard.jsμ²λΌ κΈ°λ₯λ³λ‘ JSλ₯Ό λΆλ¦¬νλ©΄ μ μ§λ³΄μμ μ 리
3. μ΄λ―Έμ§ μ μ₯μ application.propertiesμ upload.pathμ μ°λνμ¬ κ΄λ¦¬