자바스크립트

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

2025.02.22 03:17

jQuery .ajaxSetup()

  • 나우호스팅 21일 전 2025.02.22 03:17
  • 24
    0

jquery .ajaxSetup()


jQuery `.ajaxSetup()` 메서드


`.ajaxSetup()` 메서드는 jQuery의 AJAX 요청에 대한 기본 설정을 지정할 수 있습니다. 예를 들어, AJAX 요청의 기본 URL, HTTP 메서드, 데이터 형식, 오류 처리 등 다양한 설정을 지정할 수 있습니다.

# `.ajaxSetup()` 메서드의 기본 형식


`.ajaxSetup()` 메서드의 기본 형식은 다음과 같습니다.

#hostingforum.kr
javascript

$.ajaxSetup({

  url: 'default_url',

  type: 'default_type',

  dataType: 'default_dataType',

  data: 'default_data',

  success: function(data, textStatus, jqXHR) {},

  error: function(xhr, status, error) {},

  complete: function(xhr, status) {}

});



# `.ajaxSetup()` 메서드의 옵션


`.ajaxSetup()` 메서드에는 다양한 옵션이 있습니다. 이 옵션들은 다음과 같습니다.

* `url`: AJAX 요청의 기본 URL을 지정합니다.
* `type`: AJAX 요청의 기본 HTTP 메서드를 지정합니다. 예를 들어, `GET`, `POST`, `PUT`, `DELETE` 등이 있습니다.
* `dataType`: AJAX 요청의 기본 데이터 형식을 지정합니다. 예를 들어, `json`, `xml`, `script` 등이 있습니다.
* `data`: AJAX 요청의 기본 데이터를 지정합니다.
* `success`: AJAX 요청이 성공했을 때 호출되는 함수입니다.
* `error`: AJAX 요청이 실패했을 때 호출되는 함수입니다.
* `complete`: AJAX 요청이 완료했을 때 호출되는 함수입니다.

# 예제


`.ajaxSetup()` 메서드를 사용하는 예제는 다음과 같습니다.

#hostingforum.kr
javascript

$.ajaxSetup({

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

  type: 'GET',

  dataType: 'json',

  data: {

    id: 1

  },

  success: function(data) {

    console.log(data);

  },

  error: function(xhr, status, error) {

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

  },

  complete: function(xhr, status) {

    console.log('Complete: ' + status);

  }

});



$.ajax({

  type: 'GET',

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

  success: function(data) {

    console.log(data);

  }

});



# 소스 코드


`.ajaxSetup()` 메서드의 소스 코드는 다음과 같습니다.

#hostingforum.kr
javascript

// jQuery 3.6.0의 ajaxSetup() 메서드 소스 코드

ajaxSetup: function( settings ) {

  if ( this.isReady && this.active ) {

    return;

  }



  // Attach a promise and the done callback

  // to the method

  return this.then( function() {

    var options = settings || {};

    var settings = jQuery.extend( {}, this.ajaxSettings, options );



    // Attach deferreds to the settings for internal usage

    settings.deferred = jQuery.Deferred();



    // Update URL as jQuery.extend() doesn't copy it

    // the call to _load would be made with the original URL in classic jQuery

    settings.url = jQuery.correctedAjaxUrl( settings.url );



    // If not request is in progress prepare and send new request

    // Do not tie up other AJAX requests while the lock is held

    if ( !this.active ) {

      this.locked = true;



      // Only apply to non-jQuery-XHR requests for cross-domain request detection

      var request = jQuery.ajax( settings );



      // Only trigger active if the request was not aborted

      if ( request.readyState === 4 ) {

        this.locked = false;



        // Handle the original request

        this._load( settings, request, realXHR );

      }



      // Keep a copy of the request in case it's the method

      // that got rejected

      this._request( settings, request );



      // Trigger active

      this.active++;

    }



    return this;

  } );

},



# 결론


`.ajaxSetup()` 메서드는 jQuery의 AJAX 요청에 대한 기본 설정을 지정할 수 있습니다. 이 메서드는 다양한 옵션을 제공하며, AJAX 요청의 기본 URL, HTTP 메서드, 데이터 형식, 오류 처리 등 다양한 설정을 지정할 수 있습니다. `.ajaxSetup()` 메서드는 jQuery의 AJAX 요청을 보다 간편하고 효율적으로 관리할 수 있도록 도와줍니다.

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

[NGINX] default_type - 기본 MIME 타입을 설정합니다.
[PHP] stat - 파일 정보를 제공합니다
[PHP] pi - pi 값 가져오기
[PHP] log - 자연로그
[PHP] end - 배열의 내부 포인터를 마지막 요소로 설정합니다.
[PHP] dl - 런타임에 PHP 확장을 로드합니다.
[PHP] date - Unix 타임스탬프 형식 지정
[PHP] copy - 파일을 복사합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색