728x90
오늘은 제이쿼리 get, post 메서드를 사용해 보도록 하겠습니다.
1.get메서드
<button id="addget">addget</button>
<p id="get"></p>
<script>
$('#addget').click(function () {
$.get('http://localhost:3000/get', function (data) {
$('#get').append(`<br>${data}`)
})
})
</script>
get으로 요청을 보내면 받아온 데이터를 p태그에 append 해주는 코드입니다.
app.get('/get', function(req,res){
res.send('get');
})
'get' 데이터를 보냅니다.
2.post메서드
<button id="addpost">addpost</button>
<p id="post"></p>
<script>
$('#addpost').click(function(){
$.post('http://localhost:3000/post',{name : 'balmostory'}, function (data) {
$('#post').append(`<br>${data}`)
})
})
</script>
post로 {name : 'balmostory'}를 보냅니다.
그리고 받아온 데이터를 p태그에 apped 해줍니다.
app.post('/post', function(req,res){
res.send(`hi ${req.body.name}`)
})
넘어온 데이터에 hi를 추가해 보내줍니다.
728x90
'기타 > jQuery' 카테고리의 다른 글
[jQuery] 제이쿼리 ajax 예제 (0) | 2021.02.16 |
---|---|
[jQuery] 제이쿼리 이벤트 발생 click, bind, on 사용법 (0) | 2021.02.15 |
[jQuery] 제이쿼리 요소 너비 높이 구하기 width(), height() 메소드 (0) | 2021.02.14 |
[jQuery] 제이쿼리 첫번째, 마지막 요소 스타일 추가하기 by first(), last() (0) | 2021.02.13 |
[jQuery] 제이쿼리 형제 선택자 by siblings(), next() (0) | 2021.02.12 |