Language/jQuery

[AJAX] jQuery ajax 사용 소스 (feat.php)

728x90
/**
* 기능 : 리뷰 영상 데이터 가져오기
* 이름 : get_reviewmovie_data
* param () [
*    idx [integer],
*    mb_id [string]
* ]
*
* result (json data) [
*    idx [integer],
*    mb_id [string],
*    upload_date [datetime or date]
*    update_date [datetime or date]
*    subject [string]
*    content [string]
*    hash [string]
*    like_count [integer]
*    view_count [integer]
*    movie_url [string]
* ]
*/

function get_reviewmovie_data(idx, mb_id) {

    //검증 로직
    if( typeof idx == 'undefined' || idx == '' || idx < 1 ) {
        alert('문구');
        return false;
   }
    if( typeof mb_id == 'undefined' || mb_id == '' || !mb_id) {
        alert('문구');
        return false;
    }

    jQuery.post('https://www.도메인주소.com/사용할url.php', 
{idx:idx, mb_id:mb_id}, function(data) {
        var result = jQuery.parseJSON(data);
        if( typeof result == 'undefined' || 
 typeof result.success == 'undefined' || 
 typeof result.reason == 'undefined' || 
 typeof result.data == 'undefined' ){
            alert('에러 문구');
        } else {
            if( result.success == 'N' ) {
                // result.reason 에 따른 문구 분기
                alert(' 에러 문구 ');
            } else {
                var dat = result.data;
                // 데이터 처리
                alert(dat.idx);
            }
        }
        //결과값 처리
    });
}

<?php

  $arr = array();
  $arr['data'] = array();
  $arr['success'] = 'N';
  $arr['reason'] = '';

  //실패시
  $arr['reason'] = '1'; // 실패 사유에 따라 숫자 혹은 텍스트 표현
  $arr['success'] = 'N';

  echo json_encode($arr);
  exit;


  //성공시
  $arr['reason'] = '';
  $arr['success'] = 'Y';
  //데이터 베이스에서 얻은 값 or 로직에 의해 얻은 값
  $arr['data']['idx'] = '2';

  $arr['data']['mb_id'] = 'sdfsdfsdfsdf';
  .
  .

  echo json_encode($arr);
  exit;
?>
728x90