일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- evernote
- todoist
- 린나이온도조절기
- debounce
- react-native
- xcode11
- EditText
- 가스요금폭탄
- Swipes
- anydo
- Raspberry Pi
- 온도조절기
- 나비엔
- IOT
- REACT
- __attribute__
- Node
- 온도센서
- 에버노트
- 기름보일러
- nodejs
- 경동
- rasppi3
- 네모안
- 가스비절약
- 라온익스
- ReactNative
- npm moment
- 난방비절약
- node.js
- Today
- Total
어허
Node.js post로 받은 json을 그대로 사용하면 int를 int로 하지않고 string으로 처리해버림 본문
Node.js post로 받은 json을 그대로 사용하면 int를 int로 하지않고 string으로 처리해버림
AKDK 2016. 11. 21. 18:19Node.js - Express 로 웹서버를 만들고
post 를 처리하는 과정에서 req.body에 넘어온 JSON 패킷을 사용하려다 보니 문제가 발생했다.
>> client-side
function ajax_post(path, params)
{
$('#dbgRespJson').html('');
$.ajax({
url:path,
dataType:'json',
type:'POST',
//data:{'msg':$('#send_msg').val()},
data:params,
success:function(result){
dbg_log ( result );
g_RespJson = result['resp'];
$('#dbgRespJson').html(g_RespJson);
dbg_RespJson(JSON.parse(g_RespJson));
}
});
}
function get_state()
{
var getstate_io_json = {'jsonrpc': '2.0', 'method': 'getstate.generalio', 'params': [ { 'idx': 1 } ] };
console.log ("getstate_io_json :", getstate_io_json);
ajax_post('/g_device', getstate_io_json);
// dbg_log("g_RespJson = ", g_RespJson);
}
>> server-side
console.log("[POST] req.body: ", req.body);
console.log("[POST] JSON.stringify(req.body): ", JSON.stringify(req.body));
[POST] req.body: { jsonrpc: '2.0',
method: 'getstate.generalio',
params: [ { idx: '1' } ] }
[POST] JSON.stringify(req.body): {"jsonrpc":"2.0","method":"getstate.generalio","params":[{"idx":"1"}]}
디버깅을 해보면 이렇게 나온다.
>> 해결
int 형으로 사용하려고 하는 필드를 parseInt()를 사용하여 Integer 인것을 알려주어야 한다.
req.body.params[0].idx = parseInt(req.body.params[0].idx);
console.log(" [POST] req.body: ",req.body);
'개발 > JavaScript' 카테고리의 다른 글
for(let i in myArray) 에서 i는 string이다 (0) | 2019.04.16 |
---|---|
DataTable warning alert 출력 안보이게 하기 (0) | 2018.02.13 |