본문 바로가기

기타/jQuery

[jQuery]제이쿼리 get post 메소드

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