Global It Leader!!



 
 

Javasript Jquery Ajax for JSON , PHP

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 5,307회 작성일 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})");

댓글목록

등록된 댓글이 없습니다.

전체 440
게시물 검색
컴퓨터언어 목록
번호 제목 글쓴이 조회 날짜
120 PHP no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6892 03-21
119 PHP no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5041 03-21
118 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5278 03-21
117 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5225 03-21
116 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5913 03-15
열람중 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5308 03-15
114 그누보드스킨 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 17062 03-15
113 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4973 03-10
112 PHP no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5545 03-10
111 PHP no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5211 03-10
110 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5938 03-09
109 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5420 03-08
108 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4909 03-08
107 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6409 03-05
106 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5269 03-05
105 PHP no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5007 03-04
104 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4978 03-04
103 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5505 03-01
102 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5286 03-01
101 PHP no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5382 02-29