[Error] ajax 사용 중 'JSON parse error: ~'

2020. 2. 22. 03:24·이슈 해결

 

 

ajax로 이것저것 해보던 중 에러를 만났다.

 

JSON parse error: Unrecognized token 'param1': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'param1': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

 

 

 

오류 발생 ajax코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$.ajax({
    url : "[[@{/ajax/testForm1}]]",
    type : "POST",
   data : ({
        param1 : param1,
        param2 : param2,
        param3 : param3,
        param4 : param4
    }),
    contentType: "application/json",
    dataType : "json",
    success : function(data) {
        console.log("success!")
    }
});

 

 

 

원인 :

4번째 줄의 data 안에는 내가 2번째 줄에 지정해 놓은 url로 넘겨주기 원하는 값들을 data라는 곳에 넣는 구문이다.

이때 ajax는 저 data를 문자열화 해주지 않기 때문에 Controller에서 받아줄 때에 에러가 생긴다.

 

 

 

해결

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$.ajax({
    url : "[[@{/ajax/testForm1}]]",
    type : "POST",
    data : JSON.stringify ({
        param1 : param1,
        param2 : param2,
        param3 : param3,
        param4 : param4
    }),
    contentType: "application/json",
    dataType : "json",
    success : function(data) {
        console.log("success!")
    }
});

4번째 라인에 'JSON.stringify'라는 구문이 추가된게 보인다.

JSON.stringify는 JSON 객체를 String 객체로 변환 시켜주기 때문에 Controller에서 에러 없이 받아주게 된다.

 

 

 

 

'이슈 해결' 카테고리의 다른 글

Spring boot에서 'request header is too large' 해결.  (0) 2020.12.12
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set  (5) 2020.11.28
[Error] jdbcUrl is required with driverClassName.  (7) 2020.02.24
'이슈 해결' 카테고리의 다른 글
  • Spring boot에서 'request header is too large' 해결.
  • Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
  • [Error] jdbcUrl is required with driverClassName.
나는 유찌
나는 유찌
쩌리쨩
  • 나는 유찌
    유찌 개발 일기
    나는 유찌
  • 전체
    오늘
    어제
    • 분류 전체보기
      • 사이드 프로젝트
        • 게시판
        • 블로그(Spring boot + React.js ..
      • 데이터베이스
        • SQLD
      • 이슈 해결
      • Front
        • Javascript
        • Vue.js
        • HTML+CSS
      • Backend
        • Spring
        • ORM
        • JAVA
      • 공부
        • HTTP
        • OOP
        • 이것저것
        • 코딩테스트 | 알고리즘
      • Computer Science
        • Computer architecture
        • 데이터베이스
        • 운영체제
      • 일상
        • 독서
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Access token 재발급
    AntPathMatcher
    pessimisticlock
    Kotlin AntPathMatcher
    Access Token Refresh Token
    독서
    Spring Boot
    mysql 격리수준
    jwt 로그인 구현
    jwt
    access token
    role scope
    spring
    refresh token
    phantom read
    AccessDecisionVoter
    웹 개발
    Spring boot에서 JWT 구현
    히가시노 게이고
    한국소설
    spring 격리수준
    LeetCode
    Spring Security AccessDecisionManager
    redis 분산락
    DIRTY READ
    Kotlin AccessDecisionManager
    추리소설
    JWT이란?
    권한 scope 처리
    mssql
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
나는 유찌
[Error] ajax 사용 중 'JSON parse error: ~'
상단으로

티스토리툴바