웹프로그래밍

Global It Leader!!


jQuery


 
 

Jquery Ajax for JSON , PHP

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 4,801회 작성일 12-03-15 18:05

본문

ajax.html
$(document).ready(function() {
$.post('ajax_response.php', function(data) {
$('#console').empty();  /* div id = console 의 내용을 비움 */
var data_var = eval("(" + data + ")");
var json_data = "size : " + data_var.length + "<br />";
for(i=0 ; i < data_var.length; i++) {
json_data += "name : " + data_var[i].name + "<br />";
json_data += "age : " + data_var[i].age + "<br />";
}
$("#ajax_response").html(json_data);
},"json");
})  
<div id="ajax_response"></div> 

ajax_response.php
<?php
/* json_encode 함수는 (PHP 5 >= 5.2.0, PECL json >= 1.2.0) 에서 작동합니다. */
$query = mysql_query("select name, age from member");
$i= 0;
while($row = mysql_fetch_assoc($query)) {
$return_data[$i] = $row;
}
/* $return_data = ( '0' => ('name'=>'test1', age=>14), '1' => ('name'=>'test2', age=>18) ); */

echo json_encode($return_data);

?>
Example #1 json_encode() 예제
<?php
$arr 
= array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5
);

echo 
json_encode($arr);?>
위 예제의 출력:
{"a":1,"b":2,"c":3,"d":4,"e":5}
 
eval() 함수의 사용 목적은 객체를 만들기 위함이고, joson응답 또한 객체로 만들어 사용합니다.
 joson응답을 객체로 만들 때 eval함수에서 발생하는 invalid label error 때문에 괄호가 필요합니다.
이유는 eval이 처음 만나는 : 이거 전까지를 label로 해석하기 때문입니다.

label :
  statement
 이런 구조로 해석하기 때문에 문제가 발생합니다.

 그러므로 ( )를 넣어주어 label로 해석되는 문제를 해결할 수 있습니다.
 var s = eval("({'a':1, 'b':2, 'c':3})");

댓글목록

등록된 댓글이 없습니다.

전체 149
게시물 검색
jQuery 목록
번호 제목 글쓴이 조회 날짜
89 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3501 05-01
88 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3405 04-23
87 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3543 04-17
86 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3495 02-03
85 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3500 01-12
84 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3492 04-04
83 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3919 01-30
82 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3973 01-30
81 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3976 12-23
80 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3542 12-19
79 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3762 12-18
78 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4067 12-09
77 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3787 11-18
76 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3718 09-29
75 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4073 09-19
74 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3725 09-19
73 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4110 09-06
72 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3835 05-20
71 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3780 04-11
70 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3886 03-20