jQuery ajax() 메서드
예시
AJAX 요청을 사용하여 <div> 요소의 텍스트를 변경합니다.
$("button").click(function(){
$.ajax({url: "demo_test.txt", success: function(result){
$("#div1").html(result);
}});
});
정의 및 사용
ajax() 메서드는 AJAX(비동기 HTTP) 요청을 수행하는 데 사용됩니다.
모든 jQuery AJAX 메소드는 ajax() 메소드를 사용합니다. 이 방법은 다른 방법을 사용할 수 없는 요청에 주로 사용됩니다.
통사론
$.ajax({name:value, name:value, ... })
매개변수는 AJAX 요청에 대해 하나 이상의 이름/값 쌍을 지정합니다.
아래 표에서 가능한 이름/값:
Name | Value/Description |
---|---|
async | A Boolean value indicating whether the request should be handled asynchronous or not. Default is true |
beforeSend(xhr) | A function to run before the request is sent |
cache | A Boolean value indicating whether the browser should cache the requested pages. Default is true |
complete(xhr,status) | A function to run when the request is finished (after success and error functions) |
contentType | The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded" |
context | Specifies the "this" value for all AJAX related callback functions |
data | Specifies data to be sent to the server |
dataFilter(data,type) | A function used to handle the raw response data of the XMLHttpRequest |
dataType | The data type expected of the server response. |
error(xhr,status,error) | A function to run if the request fails. |
global | A Boolean value specifying whether or not to trigger global AJAX event handles for the request. Default is true |
ifModified | A Boolean value specifying whether a request is only successful if the response has changed since the last request. Default is: false. |
jsonp | A string overriding the callback function in a jsonp request |
jsonpCallback | Specifies a name for the callback function in a jsonp request |
password | Specifies a password to be used in an HTTP access authentication request. |
processData | A Boolean value specifying whether or not data sent with the request should be transformed into a query string. Default is true |
scriptCharset | Specifies the charset for the request |
success(result,status,xhr) | A function to be run when the request succeeds |
timeout | The local timeout (in milliseconds) for the request |
traditional | A Boolean value specifying whether or not to use the traditional style of param serialization |
type | Specifies the type of request. (GET or POST) |
url | Specifies the URL to send the request to. Default is the current page |
username | Specifies a username to be used in an HTTP access authentication request |
xhr | A function used for creating the XMLHttpRequest object |
직접 해보십시오 - 예
사용 비동기 설정을 사용하여 동기 요청을 지정하는 방법
dataType 설정을 사용하여 요청에 대한 데이터 유형을 지정하는 방법.
오류를 처리하기 위해 오류 설정을 사용하는 방법.