220414 - AJax(바뀜, 날씨)

김정현·2022년 4월 14일
0
  • 누를 때 마다 이미지, 메시지 바뀜
<script>
function q1() {
    $.ajax({
        type: "GET",
        url: "http://spartacodingclub.shop/sparta_api/rtan",
        data: {},
        success: function (response) {
            let url =response['url']
            let msg =response['msg']

            $('#img-rtan').attr('src',url)
            $('#text-rtan').text(msg)

        }
    })
}
  • $('#').attr('',) - 이미지 바뀜
  • $('#').text() - 메시지 바뀜
  • 바뀌는 기능은 여러가지에 응용할 수 있다고 생각된다.
  • 홈페이지를 들어가면 자동으로 날씨를 업데이트해줌
  • 자동 업데이트 - $(document).ready(function () {
<script>
        $(document).ready(function () {
            $.ajax({
                type: "GET",
                url: "http://spartacodingclub.shop/sparta_api/weather/gumi",
                data: {},
                success: function (response) {
                    let temp = response['temp']
                    $('#temp').text(temp)
                }
            })
        });

    </script>
  • temp_html에 대해서 조금 익숙해져 가는거 같다.

0개의 댓글