개발

POST방식으로 AJAX를 보낼 때, JSON parse error: Unrecognized token 'postidx'

Domaya 2023. 2. 13. 21:19
$.ajax({
				type: "post",
				url: "/board/newreply",
				data:  JSON.stringify({
					"postidx" : idx,
					"reply" : reply
				}),
				dataType:'json',
				async: true, //비동기 여부
				contentType: "application/json",
				success: function (result) {
					console.log(result);

				}
			})

컨트롤러

	// 댓글 작성
	@RequestMapping("/board/newreply")
	public ResponseEntity<Map<String, Object>> newReply(@RequestBody HashMap<String,Object> data){

		Map<String, Object> map = new HashMap<String, Object>();		
        String postidx = (String)data.get("postidx");
        String reply = (String)data.get("reply");

        int result = replyservice.newReply(postidx, reply);
        if(result > 0) {
        	map.put("성공", result);
        }else {
        	map.put("실패", result);
        }
		
		return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK);
		
	}

ajax에서 dataType을 json으로 써줬음에도

POST방식은 JSON.stringify를 써줘야 null이 아닌 올바른 값이 넘어가는 것을 확인했다.