개발

[에러일기]ajax통신 중 error:SyntaxError: Unexpected token 'c' in JSON

Domaya 2023. 10. 10. 21:17
@RequestMapping(value = "/joinus/authCheck", method = RequestMethod.POST)
	public String checkMail(@RequestBody HashMap<String, Object> data) {
			
		String email = (String) data.get("email");
		String userAuthKey = (String) data.get("userAuthKey");
			
		String result = mailService.checkAuthMail(email, userAuthKey);
		
		return result;
	}​

백단 소스

 

$.ajax({
				type : "POST",
				url : "authCheck",
				data : JSON.stringify({
					"email" : email,
					"userAuthKey" : userAuthKey
				}),
                dataType : "json"
				contentType : "application/json",
				success : function(data) {
					console.log("success")
					 if(data == "correct") {
						$('#auth_ck').empty(data);
						$('#auth_ck').append("<div id='p' style='color:green;'>올바른 인증번호</div>");
						console.log(data)
					} else if(data == "incorrect"){
						$('#auth_ck').empty(data);
						 $('#auth_ck').append("<div id='p' style='color:red;'>올바르지 않은 인증번호</div>");
					}
				},
				error : function(request,status,error) {
					console.log("에러....code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
				}

뷰단 소스

 

 

정상적으로 작동시 서버에서 "correct"라는 문자열을 리턴하도록 하였는데

ajax에서 error function에 걸렸다.

code : 200에

Unexpected token 'c' in JSON

이런 에러였는데

 

알고보니 ajax를 내가 잘못써서였다

서버에서 반환되는 데이터의 타입을 지정하는 것이 'dataType',

뷰에서 서버로 보낼 데이터의 타입을 지정하는 것이 'contentType'인데

나는 이 dataType에 JSON을 명시해놔서 생기는 문제였다