2023~/js,jquery

ajax option

yanii 2023. 5. 20. 22:47

ajax 주요 옵션들 정리

//sample
$.ajax({
        type: "GET",
        url: "api/Board/select",
        contentType: "application/json",
        dataType: "json",
        data: JSON.stringify({
                "title": document.getElementById("title").value,
                "content": document.getElementById("content").value
                })
        success: function(data, statusText, jqxhr){
            console.log(data);
            console.log(statusText);
        },
        error: function(jqxhr, textStatus, errorThrown){
            console.log(jqxhr);
        }
})
이름 내용 설명
type get - select
post - create
put - update
delete - delete
전송 방식
url   요청할 url
contentType application/x-www-form-urlencoded; charset=UTF-8
application/json
text/plain
서버에 전송되는 데이터의 형식
data   서버에 전송할 데이터
dataType text
html
xml
json
서버에서 응답으로 기대되는 데이터 유형
async true
false
기본적으로 비동기 요청을 한다(default:true)
동기 요청이 필요하면 false로 설정
success   요청 성공 시 호출되는 함수
error   요청 실패 시 호출되는 함수
beforeSend   요청이 보내기 전에 호출되는 함수
complete   요청 성공, 실패와 관계없이 완료될 때 호출되는 함수