• This project
    • Loading...
  • Sign in

김서연 / Corona_Vaccination_Medical_Institution

%ea%b7%b8%eb%a6%bc1
Go to a project
Toggle navigation Toggle navigation pinning
  • Projects
  • Groups
  • Snippets
  • Help
  • Project
  • Activity
  • Repository
  • Pipelines
  • Graphs
  • Issues 3
  • Merge Requests 0
  • Wiki
  • Snippets
  • Network
  • Create a new issue
  • Builds
  • Commits
  • Issue Boards
Closed
Open
Issue #2 opened 2021-05-22 15:16:17 UTC by 김서연@2018103585

Express에서 View페이지로 Json넘기는 방법

  • [문제1 ] : express 프레임워크에서 템플릿엔진 ejs를 이용하여 View페이지로 JSON객체 넘길때 값이 제대로 안넘어가는 문제발생 값이 [Object,Object]형식으로 제대로 나오지않았음.

  • [원인] : JSON객체는 보통 String처럼 전달하면 안된다.

  • [해결책]: 서버에서 JSON객체를 String객체로 변환하기위해 stringify메소드 이용해야함. View Page에서 값 받은후 String객체를 JSON객체로 다시 만들기위해 불필요한 코드제거 후 parse메소드 이용해야함.

  res.render('index', {result:JSON.stringify(searchList)
    var listData = "<%= result %>";
    listData = listData.replaceAll('&#34;','\"'); //불필요한 코드제거
    listData = JSON.parse(listData);


  • [문제2] ejs의 script문 안에서 서버전송값을 이용하고 싶었는데 기존 방식대로 <%= 변수이름%> 하면 먹히지않음

  • [원인] : 양옆에 " "로 감싸주지않음

  • [해결책]:

    <script>
    var listData = "<%= result %>";
    </script>
    
Edited 2021-05-27 10:15:48 UTC
  • HeoJeongYong
    @2016103567 commented 2021-05-22 16:22:14 UTC

    추가적으로 parse는 string 객체를 json 객체로, stringify는 json 객체를 string 객체로 변환시켜주는 기능을 함.

  • HeoJeongYong
    @2016103567 2021-05-22 18:08:36 UTC

    Mentioned in commit f3705117

  • HeoJeongYong
    @2016103567 2021-05-27 10:02:58 UTC

    Mentioned in merge request !13

  • HeoJeongYong
    @2016103567 2021-05-27 10:15:48 UTC

    Mentioned in commit 9d899f39

  • Please register or login to post a comment
Assignee
No assignee
Assign to
None
Milestone
None
Assign milestone
None
Due date
No due date
1
Labels
참고
Assign labels
  • View labels
2
2 participants
Reference: 2018103585/Corona_Vaccination_Medical_Institution#2