더 나은 프로그래머가 되자

jquery $.ajax에서 success callback 호출시 this 객체 사용 본문

언어/js

jquery $.ajax에서 success callback 호출시 this 객체 사용

greathuman 2022. 1. 27. 11:28

ajax 호출이 끝나고 선택된 객체의 데이터를 사용하고싶은데 ajax 안에서 기본적으로 this를 쓰면 response 데이터가 들어간다

ajax 호출전에 발생했던 이벤트의 this 객체를 사용하기 위해서는 context : this를 추가해야한다.

 

$.ajax({
	type : "POST"
	, async : true //true, false
	, url : "" //Request URL
	, dataType : ""
	, timeout : 3000 
	, cache : false
	, data : 
	,context : this //이걸 추가해야한다
	, contentType: "application/x-www-form-urlencoded; charset=UTF-8"
	, error : function(request, status, error) {
	}
	, success : function(response, status, request) {
		//this 사용가능
	}
});

위와 같이 추가하면 this 객체를 사용 할 수 있다

 

 

출처 : https://hakurei.tistory.com/87

Comments