/** * 共通APIアクセス */ var ApiAccess = { // 最終APIステータス lastApiStatus: true, /** * APIアクセス * @param apiUrl * @param paramArray * @param successFunction * @param optionArray * @param errorFunction */ access: function ( apiUrl, paramArray, successFunction, optionArray, errorFunction ) { if ( paramArray == undefined ) { paramArray = {}; } // ローディング表示 $( '#img_main_logo' ).hide(); $( '#img_loading' ).show(); //---------- // 初期値設定 //---------- var dataType = 'json'; var type = 'GET'; //------------- // オプション設定 //------------- if ( optionArray != undefined ) { // データタイプ設定 if ( 'data_type' in optionArray ) { dataType = optionArray['data_type']; } // リクエストタイプ設定 if ( 'type' in optionArray ) { type = optionArray['type']; } } //-------------- // エラー処理設定 //-------------- if ( errorFunction == undefined ) { errorFunction = function( XMLHttpRequest, textStatus, errorThrown ) { ApiAccess.lastApiStatus = false; alert( 'APIアクセスエラー!! ' + XMLHttpRequest.status + ',' + textStatus + ',' + errorThrown.message ); } } var ajaxParamArray = { type: type, url: apiUrl, data: paramArray, dataType: dataType, cache: false }; // コールバックが指定されていた場合 if ( successFunction != undefined ) { //console.log( apiUrl ); //console.log( paramArray ); return $.ajax( ajaxParamArray ) .done( function ( data, status, xhr ) { // ローディング非表示 $( '#img_main_logo' ).show(); $( '#img_loading' ).hide(); // ステータスが200のときだけ成功 if ( xhr.status === 200 ) { ApiAccess.lastApiStatus = true; successFunction( data ); } console.log( data ); } ) .fail( errorFunction ) ; } else { return $.ajax( ajaxParamArray ); } } };