티스토리 뷰

Learned!

[ajax] 220919

kirinman 2022. 9. 19. 15:40

참고! Ajax는 jQuery를 임포트한 페이지에서만 동작 가능합니다.

즉, http://google.com/ 과 같은 화면에서 개발자도구를 열면,

jQuery가 임포트 되어있지 않기 때문에 아래와 같은 에러가 뜹니다.

Uncaught TypeError: $.ajax is not a function → ajax라는 게 없다는 뜻

 

 

$.ajax({
  type: "GET", // GET 방식으로 요청한다.
  url: "<http://spartacodingclub.shop/sparta_api/seoulair>",
  data: {}, // 요청하면서 함께 줄 데이터 (GET 요청시엔 비워두세요)
  success: function(response){ // 서버에서 준 결과를 response라는 변수에 담음
    console.log(response) // 서버에서 준 결과를 이용해서 나머지 코드를 작성
  }
})

 

 

ajax기본 골격

 

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

 

 

ex)

 

$(document).ready(function () {
     $.ajax({
    type: "GET",
    url: "http://spartacodingclub.shop/sparta_api/weather/seoul",
    data: {},
    success: function (response) {
        let temp = response['temp']

        $('#seoultemp').text(temp)
    }
});
댓글