자바스크립트

웹을 움직이는 언어, 자바스크립트! ES6+, Vue, React, Node.js 등 모던 JS 생태계를 깊이 있게 탐구하는 공간입니다.

2025.02.22 03:08

AJAX 요청을 위한 jQuery $.ajax() 함수: 개념과 예시

  • 나우호스팅 21일 전 2025.02.22 03:08
  • 18
    0

jquery .ajax()


jQuery .ajax() 함수

jQuery의 `.ajax()` 함수는 HTTP 요청을 보내고, 서버에서 데이터를 가져올 때 사용하는 함수입니다. 이 함수를 사용하면 서버와의 통신을 쉽게 할 수 있습니다.

기본 구조

`.ajax()` 함수의 기본 구조는 다음과 같습니다.

#hostingforum.kr
javascript

$.ajax({

  type: 'GET' | 'POST' | 'PUT' | 'DELETE',

  url: '서버 주소',

  data: '보낼 데이터',

  success: function(data) {

    // 성공 시 호출되는 함수

  },

  error: function(xhr, status, error) {

    // 실패 시 호출되는 함수

  }

});



속성 설명

- `type`: HTTP 요청의 타입을 지정합니다. 기본값은 `GET`입니다.
- `url`: 서버 주소입니다.
- `data`: 서버로 보낼 데이터입니다. 객체나 문자열로 전달할 수 있습니다.
- `success`: 서버에서 데이터를 성공적으로 가져올 때 호출되는 함수입니다.
- `error`: 서버에서 데이터를 가져올 때 실패할 때 호출되는 함수입니다.

예제

#hostingforum.kr
javascript

// GET 요청

$.ajax({

  type: 'GET',

  url: 'https://api.example.com/data',

  success: function(data) {

    console.log(data);

  },

  error: function(xhr, status, error) {

    console.log('Error: ' + error);

  }

});



// POST 요청

$.ajax({

  type: 'POST',

  url: 'https://api.example.com/data',

  data: { name: 'John', age: 30 },

  success: function(data) {

    console.log(data);

  },

  error: function(xhr, status, error) {

    console.log('Error: ' + error);

  }

});



jQuery .ajax() 함수의 옵션

`.ajax()` 함수에는 여러 옵션이 있습니다. 예를 들어, `async` 옵션은 요청을 동기적으로 처리하는지 비동기적으로 처리하는지 결정합니다.

#hostingforum.kr
javascript

$.ajax({

  type: 'GET',

  url: 'https://api.example.com/data',

  async: false, // 동기적으로 처리

  success: function(data) {

    console.log(data);

  },

  error: function(xhr, status, error) {

    console.log('Error: ' + error);

  }

});



jQuery .ajax() 함수의 콜백 함수

`.ajax()` 함수의 콜백 함수는 두 가지 종류가 있습니다. `success` 함수는 서버에서 데이터를 성공적으로 가져올 때 호출되는 함수입니다. `error` 함수는 서버에서 데이터를 가져올 때 실패할 때 호출되는 함수입니다.

#hostingforum.kr
javascript

$.ajax({

  type: 'GET',

  url: 'https://api.example.com/data',

  success: function(data) {

    console.log(data);

  },

  error: function(xhr, status, error) {

    console.log('Error: ' + error);

  }

});



jQuery .ajax() 함수의 데이터 형식

`.ajax()` 함수의 데이터 형식은 두 가지 종류가 있습니다. `json` 형식은 JSON 데이터를 전달할 때 사용합니다. `text` 형식은 문자열 데이터를 전달할 때 사용합니다.

#hostingforum.kr
javascript

$.ajax({

  type: 'GET',

  url: 'https://api.example.com/data',

  dataType: 'json', // json 형식

  success: function(data) {

    console.log(data);

  },

  error: function(xhr, status, error) {

    console.log('Error: ' + error);

  }

});



jQuery .ajax() 함수의 캐시

`.ajax()` 함수의 캐시 옵션은 요청을 캐시할지 결정합니다. 기본값은 `true`입니다.

#hostingforum.kr
javascript

$.ajax({

  type: 'GET',

  url: 'https://api.example.com/data',

  cache: false, // 캐시를 사용하지 않음

  success: function(data) {

    console.log(data);

  },

  error: function(xhr, status, error) {

    console.log('Error: ' + error);

  }

});



이러한 옵션과 콜백 함수를 사용하면 `.ajax()` 함수를 더 유용하게 사용할 수 있습니다.


이 게시물에 포함된 라이브러리

[PHP] stat - 파일 정보를 제공합니다
[PHP] pi - pi 값 가져오기
[PHP] log - 자연로그
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 자바스크립트 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 35건 / 1 페이지

검색

게시물 검색