본문 바로가기
즐거움 넘치는 IT 세계/코드로 만드는 디자인

[ jquery 기초 ] jquery ajax

by 디리씨 2019. 10. 13.
728x90

 

| Ajax?

      1. Asynchronous JavaScript and XML 의 약자
      2. 자바스크립트를 이용해서 비동기식으로 서버와 통신하는 방식. 이 때 XML을 이용한다.
      3. 꼭 XML을 이용할 필요는 없고, 최근에는 json을 더 많이 이용한다.
      4. 비동기식이란 여러가지 일이 동시적으로 발생한다는 뜻으로, 서버와 통신하는 동안 다른 작업을 할 수 있다는 의미.

| .ajax(settings)

      1. jQuery를 이용한 ajax통신의 가장 기본적인 API
      2. 주요속성
        1. data : 서버에 전송할 데이터, key/value 형식의 객체
        2. dataType : 서버가 리턴하는 데이터 타입 (xml, json, script, html)
        3. type : 서버로 전송하는 데이터의 타입 (POST, GET)
        4. url : 데이터를 전송할 URL
        5. success : ajax통신에 성공했을 때 호출될 이벤트 핸들러

 

예제)

<!DOCTYPE html>
<html>
<head>
	<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
	<div id="result"></div>
	<input type="text" id="msg" />
	<input type="button" value="get result" id="getResult" />
	<script>
		$('#getResult').click( function() {
			$('#result').html('');
			$.ajax({
				url:'http://opentutorials.org/example/jquery/example.jquery.ajax.php',
				dataType:'json',
				type:'POST',
				data:{'msg':$('#msg').val()},
				success:function(result){
					if(result['result']==true){
						$('#result').html(result['msg']);
					}
				}
			});
		})
	</script>
</body>
</html>

 

728x90
반응형