사용하는 브라우저 간의 차이점을 알아서 처리함. 따라서 개발자는 브라우저 상관없이 동일하게 코딩 가능
1. jQery.ajax(url[,settings])
2. jQery.ajax([settings]) -> 내가 사용할 방식
이때, 2번 방식은 url없이 객체만 전달하므로 settings 내부에 지정하거나 Global Ajax Event Handlers를 사용해야 함
$.ajax(
{
url: "https://fiddle.jshell.net/favicon.png",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
}
)
위의 코드에서 {url: "https://fiddle.jshell.net/favicon.png",
beforeSend: function( xhr ) {xhr.overrideMimeType( "text/plain; charset=x-user-defined" ); } } 는 객체settings(프로퍼티)에 해당함.
이때, 객체 settings란 Ajax 통신을 위한 옵션들을 갖는다.
옵션 종류 : data, dataType, success, type
data - 서버로 전송할 데이터
dataType - 서버로부터 회신받을 데이터(결과) 형식 [ xml | json | script | html ]
seccess - 성공 시 호출할 콜백
type - 데이터 전송 방법 [ Get | Post ] - default는 Get방식
<body>
<form>
<select name="timezone">
<option value="Asia/Seoul">asia/seoul</option>
<option value=America/New_york">america/new_york</option>
</select>
<select name="format">
<option value="Y-m-d H:i:s">Y-m-d H:i:s</option>
<option value="Y-m-d">Y-m-d</option>
</select>
</form>
<input type="button" id="bt" value="버튼"/>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$('#bt').click(function() {
$.ajax({
url:'./timezone_json.php',
type:'post',
data:$('form').serialize(),
success:function(data){
$('#time').text(data);//성공 시, id값이 time인 엘리먼트에 data 내용 입력
}
})
})
</script>
</body>
<body>
<p id="timezones"></p>
<input type="button" id="bt" value="버튼"/>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$('#bt').click(function() {
$.ajax({
url:'./timezone_json.php',
dataType:'json',
success:function(data){
var str = '';
for(var name in data){
str += '<li>'+data[name]+'</li>';
}
${'#timezones'}.html('<ul>'+str+'</ul>');
}
})
})
</script>
</body>