Trouble-Shooting

Ajax response data๊ฐ€ ์ •์ƒ์ ์ด์ง€ ์•Š์€ html text๊ฐ€ ๋„˜์–ด์˜ฌ ๋•Œ

์ฑ”๐Ÿป 2024. 1. 24. 16:18

์ž‘์„ฑ์ผ์ž: 2022๋…„ 12์›” 15์ผ

โœ”๊ฒฐ๋ก 


๐Ÿšฉ๋“ค์–ด๊ฐ€๋ฉฐ

๐Ÿ‘€๋ฌธ์ œ ์ •์˜

๋‚ด๊ฐ€ ๋ณด๋‚ธ ๋ฐ์ดํ„ฐ

BUObject<String, String> message = new BUObject<>();
//        sendJson.sendMessage(message);
message.put("message", "์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.");
message.put("code", "0");
message.put("status", "Success");

BUObjectToJson sendJson = new BUObjectToJson(response);
sendJson.sendMessage(message);

์‹ค์ œ response โ†’ html text๊ฐ€ ๋„˜์–ด์˜ดโ€ฆ

\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "htt

๐Ÿคฆโ€โ™‚๏ธ๊ด€๋ จ๋œ ์‚ฌ์‹ค ์ˆ˜์ง‘ & โ“์›์ธ ์ถ”๋ก 

parse ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ์ด์œ 

โ†’ dataType(response ๋ฐ์ดํ„ฐ์˜ type)์ด JSON์ธ๋ฐ response data๋Š” jsp ํ™”๋ฉด์ž„

โ†’ ๋‹น์—ฐํžˆ JSON ํฌ๋งท์ด ์•„๋‹ˆ๋‹ˆ ์—๋Ÿฌ๊ฐ€๋‚จ

โ†’ response data๊ฐ€ jsp ํ™”๋ฉด์ด ๋‚˜์˜ค๋Š” ์ด์œ ๋ฅผ ์•Œ์•„์•ผ ํ•œ๋‹ค.

โ†’ ์›น์—์„œ ๋‚ด๊ฐ€ ๋งŒ๋“  ์ €๊ฑฐ ์“ด๋А ํ™”๋ฉด ํ™•์ธํ•ด๋ณด๊ณ  ์žฅ๋ฐ”๊ตฌ๋‹ˆ ํ™”๋ฉด๊ณผ ๋กœ์ง์ด ๋ญ๊ฐ€ ๋‹ค๋ฅธ์ง€ ๋น„๊ตํ•ด๋ณด๊ธฐ

โ†’ ๋‹ค๋ฅธ ๊ฒŒ ์—†์Œ ใ… .ใ… โ€ฆ. request ์ชฝ๋งŒ ์ข€ ๋‹ค๋ฅผ ๋ฟ..

PrintWriter out = response.getWriter();

JSONObject test = new JSONObject();
test.put("result", "true");

response.setContentType("application/json");
out.print(test.toString());

์ด๊ฒƒ๋„ ์•ˆ๋จ

//        BUObjectToJson sendJson = new BUObjectToJson(response);
//        BUObject<String, String> message = sendJson.setMessage(true);
//        System.out.println("#### message ####" + message);
//        sendJson.sendMessage(message);

์ด๊ฑฐ ์ฃผ์„ ์ฒ˜๋ฆฌํ•ด๋„ ๊ฑ jsp ํ™”๋ฉด response๋กœ ๋‚ ์•„์˜ด


โœ”์›์ธ & ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

web.xml์— url-pattern์ด 2๊ฐœ๊ฐ€ ์žˆ์Œ(*.do, *.json)

๊ธฐ์กด url์€ .do๋กœ ๋˜์–ด์žˆ์—ˆ๋Š”๋ฐ .json์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋‹ˆ ๋จ ์™œ do๋Š” ์•ˆ ๋˜๊ณ  json์€ ๋˜๋Š”์ง€๋Š” ์•„์ง ์›์ธ ๋ชป ์ฐพ์Œ..

โ†’ 22.12.27. ์›์ธ ์ฐพ์Œ!

siteMash๋ผ๋Š” ์›นํŽ˜์ด์ง€ ๊ณตํ†ต ๋ ˆ์ด์•„์›ƒ ํ”„๋ ˆ์ž„์›Œํฌ์ธ๋ฐ ์—ฌ๊ธฐ ์„ค์ •ํŒŒ์ผ์— pattern์ด *.json๋งŒ ์žˆ์Œ

๊ทธ๋ž˜์„œ siteMash์“ฐ๋Š” ํ”„๋ก ํŠธ๋งŒ .do๊ฐ€ ์•ˆ ๋๋˜๊ฑฐ ๋ฐฑ์˜คํ”ผ์Šค๋Š” .do, .json ๋‘˜ ๋‹ค ์ž˜ ๋์Œ


Uploaded by N2T