달력

42025  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

ajax로 서버에 요청하기, 서버에서 파일읽어오기, 로컬의 파일읽어오기가 가능하다. 

url을 어떤걸 주느냐에 따라 responseText에 원하는 값이 넘어오는것이다. 

서버파일은 읽어오기를 안해봤지만 특정 xml이나 text 파일 경로를 그냥 주면 될듯... 

아래는 http request를  rapping해놓은 함수.. 굳이.. 이렇게 만들 필요는 없을듯 한데..

하여간 나중에 참고하기 위해 남겨둔다.. 

function loadStateDate(number) {
	var filePath = "data/stage-" + number + ".txt";
	reqHttpData(filePath, onLoadHttpData);
}

function reqHttpData(url, callback) {
	if( httpRequest == null ) {
		httpRequest = getHttpRequest();
		if( httpRequest == null )
			return;
	}
	httpRequest.open("GET", url, true);
	httpRequest.onreadystatechange = callback;
	httpRequest.send(null);
}

function getHttpRequest() {
	var httpReq = null;
	try {
		var httpReq = new XMLHttpRequest();
	} catch(err) {
		httpReq = null;
	}
	return httpReq;
}

function onLoadHttpData() {
	if( httpRequest.readyState != 4 ) {
		return;
	}

	var fileData = httpRequest.responseText;
	readStateData(fileData);
	drawCanvas();
}


Posted by 행복한삶~!!
|