Javascript & JQuery & CSS > ajax(), get(), post() - Ajax »ç¿ë¹ý
µî·ÏÀÏ : 2017-07-14 16:50
Á¶È¸¼ö : 147,396
1. ajax()
Jquery ¿¡¼ Ajax »ç¿ë¹æ¹ý ÀÔ´Ï´Ù.
var url_val = "/test/t01.asp?bbb=Å×½ºÆ®";
$.ajax( {
type: "POST",
url: url_val,
success : Whensuccess,
error : WhenError
});
function Whensuccess(xml)
{
alert(xml);
$("#aaa").append(xml);
}
function WhenError()
{
alert("Error");
}
¹®¹ýÀ» °£´ÜÈ÷ »ìÆì º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.
$.ajax({
type : "POST" //"POST", "GET"
, async : true //true, false
, url : "Inquiry.jsp" //Request URL
, dataType : "html" //Àü¼Û¹ÞÀ» µ¥ÀÌÅÍÀÇ Å¸ÀÔ ("xml", "html", "script", "json"µî ÁöÁ¤ °¡´É)- ¹ÌÁöÁ¤½Ã ÀÚµ¿ ÆÇ´Ü
, timeout : 30000 //Á¦Çѽð£ ÁöÁ¤
, cache : false //true, false
, data : $("#inputForm").serialize() //¼¹ö¿¡ º¸³¾ ÆĶó¸ÞÅÍ
//form¿¡ serialize() ½ÇÇà½Ã a=b&c=d ÇüÅ·Π»ý¼ºµÇ¸ç ÇѱÛÀº UTF-8 ¹æ½ÄÀ¸·Î ÀÎÄÚµù
//"a=b&c=d" ¹®ÀÚ¿·Î Á÷Á¢ ÀÔ·Â °¡´É
//{a:b, c:d} json Çü½Ä ÀÔ·Â °¡´É
, contentType: "application/x-www-form-urlencoded; charset=UTF-8"
, error : function(request, status, error) {
//Åë½Å ¿¡·¯ ¹ß»ý½Ã ó¸®
alert("code : " + request.status + "\r\nmessage : " + request.reponseText);
if (request.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (request.status == 404) {
msg = 'Requested page not found. [404]';
} else if (request.status == 500) {
msg = 'Internal Server Error [500].';
} else if (error === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (error === 'timeout') {
msg = 'Time out error.';
} else if (error === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + request.responseText;
}
alert(msg);
}
, success : function(response, status, request) {
//Åë½Å ¼º°ø½Ã ó¸®
$('#resultDIV').append(response);
}
, beforeSend: function() {
//Åë½ÅÀ» ½ÃÀÛÇÒ¶§ ó¸®
$('#ajax_indicator').show().fadeIn('fast');
}
, complete: function() {
//Åë½ÅÀÌ ¿Ï·áµÈ ÈÄ Ã³¸®
$('#ajax_indicator').fadeOut();
}
});
2. get()
ÃÊ°£´Ü »ùÇà ¿¹Á¦¸¦ ÁغñÇß´Ù.
µÎ¸» ÇÊ¿ä ¾øÀÌ ÆÄÀÏ ¸¸µé¾î¼ ½ÇÇà½ÃÄѼ °á°ú º¸¸é µÊ.
---------------------------------------------------------
get.asp
$("button").click(function(){
$.get("test.asp", function(data, status){
alert("Data °ªÀº : " + data + "\n" + "Status °ªÀº : " + status);
});
});
---------------------------------------------------------
test.asp
---------------------------------------------------------
<%@ CODEPAGE=65001%>
<%session.CodePage = "65001" %>
<%response.charset = "utf-8"%>
response.write "test.asp ÀÇ ³»¿ë ÀÔ´Ï´Ù."
3. post()
---------------------------------------------------------
get.asp
$("button").click(function(){
$.post("test.asp",
{
name:"davidseo",
city:"gimpo"
}, function(data, status){
alert("Data °ªÀº : " + data + "\n" + "Status °ªÀº : " + status);
});
});
---------------------------------------------------------
test.asp
---------------------------------------------------------
<%@ CODEPAGE=65001%>
<%session.CodePage = "65001" %>
<%response.charset = "utf-8"%>
response.write request("name") & ": À̸§ ÀÔ´Ï´Ù."