Notice
Recent Posts
Recent Comments
«   2024/04   »
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
Archives
Today
Total
관리 메뉴

하이오리

[jQuery] getJSON() 본문

jQuery/자주쓰는함수

[jQuery] getJSON()

하이오리 2013. 6. 23. 20:58

.getJSON()

문법은 .post()와 비슷하다.

.ajax()메서드의 단축표기이며, 몇 가지 매개변수가 셋팅된 것이다.

$.getJSON(url_to_load, function(json){ ... });

$.ajax({

url: url_to_load,

dataType: 'json',

data: json,

success: function(json){

...

};

}); 

 

html

<script type="text/javascript">

$(document).ready(function(){

$('#frm_bt').click(function(){

$.getJSON("json_encode_jq.php", function(json){

if(json.result.length>0){

$.each (json.result,function(){

var info = "color : "+ this['color'] + " | option : "+ this['option'] +" <br /> ";

$('#frm').append(info);

});

}

});

});

});

</script>

<form id ="frm">

<input type="button" id="frm_bt" value="전송">

</form>

 

php

<?

//php 5.2 이상에서만 가능하다.

$result=array();

array_push($result,array('color'=>"red",'option'=>"1"));

array_push($result,array('color'=>"blue",'option'=>"2"));

array_push($result,array('color'=>"green",'option'=>"3"));

echo json_encode(array("result" => $result));

?>

http://www.filelink.kr/practice/jquery/json_encode.php