같은 예금 창구에서도 개인 고객이냐 기업 고객이냐에 따라
가져와야 하는 것 / 처리해주는 것이 다른 것처럼,
클라이언트가 요청 할 때에도, "타입"이라는 것이 존재합니다.
GET
→ 통상적으로! 데이터 조회(Read)를 요청할 때
예) 영화 목록 조회
POST
→ 통상적으로! 데이터 생성(Create), 변경(Update), 삭제(Delete) 요청 할 때
예) 회원가입, 회원탈퇴, 비밀번호 수정
?
: 여기서부터 전달할 데이터가 작성된다는 의미입니다.
&
: 전달할 데이터가 더 있다는 뜻입니다.
예시) google.com/search?
q=아이폰&
sourceid=chrome&
ie=UTF-8
위 주소는 google.com의 search 창구에 다음 정보를 전달합니다!
q=아이폰 (검색어)
sourceid=chrome (브라우저 정보)
ie=UTF-8 (인코딩 정보)
Ajax
는 jQuery를 임포트한 페이지에서만 동작 가능합니다.즉, http://google.com/ 과 같은 화면에서 개발자도구를 열면, jQuery가 임포트 되어있지 않기 때문에 아래와 같은 에러가 뜹니다.
Uncaught TypeError: $.ajax is not a function
→ ajax라는 게 없다는 뜻
$.ajax({
type: "GET", // GET 방식으로 요청
url: "여기에URL을입력",
data: {}, // 요청하면서 함께 줄 데이터 (GET 요청시엔 비워두세요)
success: function(response){
console.log(response)
}
})
실시간 서울시 미세먼지 API
http://spartacodingclub.shop/sparta_api/seoulair
예제) 모든 구의 미세먼지 값 찍기
$.ajax({
type: "GET",
url: "http://spartacodingclub.shop/sparta_api/seoulair",
data: {},
success: function (response) {
let mise_list = response["RealtimeCityAir"]["row"];
for (let i = 0; i < mise_list.length; i++) {
let mise = mise_list[i];
let gu_name = mise["MSRSTE_NM"];
let gu_mise = mise["IDEX_MVL"];
console.log(gu_name, gu_mise);
}
}
});
예제2) 업데이트 눌렀을 때, 실시간 미세먼지 수치가 40 이상인 곳은 빨간색으로 표시.
<script>
function q1() {
$('#names-q1').empty()
console.clear()
$.ajax({
type: "GET",
url: "http://spartacodingclub.shop/sparta_api/seoulair",
data: {},
success: function (response) {
let rows = response['RealtimeCityAir']['row']
for (let i = 0; i < rows.length; i++) {
let gu_name = rows[i]['MSRSTE_NM']
let gu_mise = rows[i]['IDEX_MVL']
console.log(gu_name, gu_mise)
let temp_html = ''
if (gu_mise > 40) {
temp_html = `<li class="bad">${gu_name} : ${gu_mise}</li>`
} else {
temp_html = `<li>${gu_name} : ${gu_mise}</li>`
}
$('#names-q1').append(temp_html)
}
}
})
}
</script>
console.clear()
: 콘솔로그 지우기
<li class="bad">
~</li>
: 색 입히기
$('#names-q1').append(temp_html)
:html 추가
<script>
function q1() {
$.ajax({
type: "GET",
url: "http://spartacodingclub.shop/sparta_api/rtan",
data: {},
success: function(response){
let imgurl = response['url'];
$("#img-rtan").attr("src", imgurl);
let msg = response['msg'];
$("#text-rtan").text(msg);
}
})
}
</script>
$('#~').attr
('변경전이미지','변경후이미지')
$('#~').text
('변경주소','변경후주소')