2016년 3월 8일 화요일

[AJAX 활용]$.getJSON을 이용한 데이타 사용 방법

[자료 출처] http://gabrieleromanato.name/jquery-getjson-example-and-tutorial/

JSON can be used as a lightweight exchange format on many web contexts. One of its many implementations is requesting data from a database and showing the results as HTML. jQuery provides us with the $.getJSON() method to be used in these cases.

JSON은 웹 환경에서 가벼운 데이타교환용으로 사용될 수 있습니다. 많은 구현중 하나가 데이타베이스로부터 데이타를 요구하고 결과로써 이를 HTML로 보여주는 것입니다.  JQuery는 다음 경우에 사용될 수는 $.getJSON 함수를 제공합니다.


An AJAX request (usually a GET request) must be sent to a server-side script which processes the query and returns an output in JSON format. In this case we’re going to use a mock PHP script that simply returns a definition:

AJAX 요청은 서버측에 쿼리로 보내어지고 결과값으로 JSON 형태의 데이타를 돌려 받습니다. 

아래의 경우는 임의의 PHP 스크립트를 사용하여 간단히 돌려받는 방법입니다.

<서버측 스크립트 언어>
<?php
//이 헤더문구가 꼭 필요합니다. 아니면 항상 null값을 가지게 될 것입니다.
header('Content-Type: application/json');
$request = strtolower($_REQUEST['word']);

if(!ctype_alpha($request)) {

 $error = array('error' => 'Not a valid word');
 echo json_encode($error);
 
 exit();

}

$words = array(
            'css' => 'A stylesheet language',
            'dom' => 'A document model interface',
            'xml' => 'A structured markup language',
            'javascript' => 'A client-side web programming language'
         );
         
if(array_key_exists($request, $words)) {

 $output = array('definition' => $words[$request]);
 
 echo json_encode($output);

}
?>
 더 많은 내용은 사이트를 참조하시면 되겠습니다.

아래 동영상 사이트를 참조하셔도 좋을 듯 합니다.

https://www.youtube.com/watch?v=p55i-_1x0lo&feature=youtu.be


댓글 없음:

댓글 쓰기