자주 써도 헷갈리는 ajax 스크립트 한번에 정리
GET
- 서버에서 데이터 조회
$.ajax({
url: url
type: 'GET',
async: true, // 기본값은 비동기, false일 경우 동기 요청으로 변경
dataType : 'json', // 서버로부터 수신할 데이터 타입 xml, script, html, text..
beforeSend: function (xhr) {
// ajax 요청 전 실행되는 내용
}, success: function (result) {
// 성공 시 수행할 내용
}, error: function (error) {
// 실패 시 수행할 내용
}, complete: function(data) {
// 성공 실패와 상관없이 항상 수행할 내용
}
});
POST / PUT
- 서버에 데이터 전송
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json', // 전송할 데이터 타입
// 기본 값 : "application / x-www-form-urlencoded; charset = UTF-8"
data: JSON.stringify({
// 전송할 데이터
}),
beforeSend: function (xhr) {
// ajax 호출 전 실행되는 내용
},
success: function (result) {
// 성공 시 수행할 내용
}, error: function (error) {
// 실패 시 수행할 내용
}, complete: function(data) {
// 성공 실패와 상관없이 항상 수행할 내용
}
});
$.ajax ({
url : url,
type: 'GET',
dataType: 'json',
...
})
.done(function(){
// 요청 성공 시 실행할 콜백함수
})
.fail(function(){
// 요청 실패 시 실행할 콜백함수
})
.always(function(){
// 성공 실패와 상관없이 항상 호출
})
성공시 호출 순서 : success -> complete -> done -> always
실패시 호출 순서 : error -> complete -> fail -> always
jQuery API Documentation
jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. If you're new t
api.jquery.com
success()
A function to be called if the request succeds.
.ajaxSuccess() (success & .ajax() option)
Attach a function to be executed whenever an Ajax request completes successfully. This is an Ajax Event
.done()
Add hanlders to be called when the Deferred object is resolved
[stackoverflow] https://stackoverflow.com/questions/8840257/jquery-ajax-handling-continue-responses-success-vs-done
jQuery.ajax handling continue responses: "success:" vs ".done"?
I have been working with jQuery and AJAX for a few weeks now and I saw two different ways to 'continue' the script once the call has been made: success: and .done. From the synopsis from the jQuery
stackoverflow.com
[JQUERY] 동적으로 생성된 태그에 이벤트 주기 (0) | 2021.11.08 |
---|---|
[JQUERY] 키보드 이벤트 keydown, keyup, keypress (0) | 2021.08.26 |
[DOM] html 태그 input type="password" 관련 경고 메시지 (0) | 2021.08.26 |
[REGEX] 회원가입 정규식(2) 비밀번호 (0) | 2021.04.29 |
[REGEX] 회원가입 정규식(1) (0) | 2021.04.23 |
댓글 영역