Javascript & JQuery & CSS > JSONÆÄÀÏ Àоî¿À±â2 - ±¸Á¶°¡ ´Ù¸¥ ¹®ÀÚ¿ Àбâ
µî·ÏÀÏ : 2017-07-14 17:05
Á¶È¸¼ö : 53,518
.post()¿Í ºñ½ÁÇÏ´Ù.
.ajax()¸Þ¼µåÀÇ ´ÜÃàÇ¥±âÀ̸ç, ¸î °¡Áö ¸Å°³º¯¼ö°¡ ¼ÂÆÃµÈ °ÍÀÌ´Ù.
¾Æ·¡ ¼Ò½º¸¦ Å×½ºÆ® ÇØ º¸»ï.
data.asp ÆÄÀϳ»¿ë
-------------------------------------
{"counter1":"1",
"content1":[{ "Num":"1", "Name":"È«±æµ¿" },
{ "Num":"2", "Name":"¹éµÎ»ê" }],
"content2":[{ "Num":"1", "Name":"È«±æµ¿" },
{ "Num":"2", "Name":"¹éµÎ»ê" }],
"counter2":"2"
}
--------------------------------------------
test.html ÆÄÀϳ»¿ë
---------------------------------------------
$(document).ready(function(){
ajaxOptions = {
url: '/test/data.asp',
category_id: 'A01',
cnt: '20',
area_code: '',
page: '1'
};
$("#abcde").click(function(){
$.ajax( {
type: "POST",
url: ajaxOptions.url,
dataType : 'json',
data : ajaxOptions,
success : Whensuccess,
error : WhenError
});
function Whensuccess(xml)
{
alert(xml.counter1);
$.each(xml.content1, function(index, type) {
alert(" "+ type.Name + " ");
});
$.each(xml.content2, function(index, type) {
alert(" "+ type.Name + " ");
});
alert(xml.counter2);
}
function WhenError(xhr, textStatus, errorThrown)
{
alert(errorThrown);
}
});
});
counter1 : 1
counter2 : 2
content1 :[
{ "Num":"1", "Name":"È«±æµ¿"},
{ "Num":"2", "Name":"¹éµÎ»ê"}
]
content2 :[
{ "Num":"1", "Name":"È«±æµ¿"},
{ "Num":"2", "Name":"¹éµÎ»ê"}
]
À§¿Í °°ÀÌ ±¸Á¶°¡ ´Ù¸¥ JSON ÆÄÀÏÀÌ Á¸ÀçÇÕ´Ï´Ù.
ÀÌ·±°æ¿ì data.asp ÆÄÀÏ¿¡ À§¿Í °°Àº ÇüÅ·ΠÀúÀåÇØ µÓ´Ï´Ù.
ajax() À» ÀÌ¿ëÇØ JSON ÆÄÀÏÀ» ÀÐÀ» °æ¿ì, xml.counter1 , xml.counter2 ó·³ ÀÐÀ» ¼ö ÀÖÀ¸¸ç,
¹®ÀÚ¿ JSON ÀÇ °æ¿ì¿¡´Â
$.each(xml.content1, function(index, type) {
alert(" "+ type.Name + " ");
});
$.each(xml.content2, function(index, type) {
alert(" "+ type.Name + " ");
});
¿Í °°Àº ÇüÅ·ΠÀоî¿À¸é µÇ°Ú½À´Ï´Ù.