Ajax 기본 골격

min j·2021년 8월 12일
0

Ajax 기본 골격

$.ajax({
  type: "GET",
  url: "여기에URL을입력",
  data: {},
  success: function(response){
    console.log(response)
  }
})

만일
Uncaught TypeError: $.ajax is not a function
이런 에러가 뜬다면 jQuery가 임포트 되어 있지 않아서다.
Ajax는 jQuery가 임포트 된 페이지에서만 작동한다.

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

(제이쿼리 임포트 코드)
출처 : https://www.w3schools.com/jquery/jquery_get_started.asp

Ajax 기본 골격 코드 해설

$.ajax({
  type: "GET", // GET 방식으로 요청한다.
  url: "http://openapi.seoul.go.kr:8088/6d4d776b466c656533356a4b4b5872/json/RealtimeCityAir/1/99",
  data: {}, // 요청하면서 함께 줄 데이터 (GET 요청시엔 비워두세요)
  success: function(response){ // 서버에서 준 결과를 response라는 변수에 담음
    console.log(response) // 서버에서 준 결과를 이용해서 나머지 코드를 작성
  }
})
  • type: "GET" → GET 방식으로 요청한다.

  • url: 요청할 url

  • data: 요청하면서 함께 줄 데이터 (GET 요청시엔 비워두세요)

    리마인드
    GET 요청은, url뒤에 아래와 같이 붙여서 데이터를 가져갑니다.
    http://naver.com?param=value¶m2=value2

    POST 요청은, data : {} 에 넣어서 데이터를 가져갑니다.
    data: { param: 'value', param2: 'value2' },

  • success: 성공하면, response 값에 서버의 결과 값을 담아서 함수를 실행한다. 결과가 어떻게 response에 들어가는지 이해하려고 하지 말고 그냥 받아들이자!

success: function(response){ // 서버에서 준 결과를 response라는 변수에 담음
  console.log(response) 
}
profile
나는 장민이다

0개의 댓글