2016-11-23 3 views
0

私はAPIを作成しています。これは私のPHP関数です。構造関数を使用したAJAX呼び出しの解析エラーの取得

function get_schools($cn){ 
    $schools = "SELECT * FROM schools"; 
    $school_result = mysqli_query($cn, $schools); 
    $response_array['form_data']['schools'] = ''; 
    $school_array = array(); 
    while ($school_row = mysqli_fetch_assoc($school_result)) { 
     $school_array[$school_row['school_id']] = $school_row['school_name']; 
    } 
    $response_array['status'] = 'success'; 
    $response_array['form_data']['schools'] = $school_array; 
    echo json_encode($response_array); 
} 

これは私のjsです。

getSchools(); 

function getSchools(){ 
    var requestDATA = { 
     'request': 'get_schools' 
    } 
    myApp.showIndicator(); 
    serverRequest(requestDATA, getSchoolsSuccess, responseError, true, true); 
} 

function getSchoolsSuccess(){ 
    if(hideIndicator){ 
     myApp.hideIndicator(); 
    } 
    console.log(data); 

    if(data.status == 'success'){ 

    }else if (data.status == 'error'){ 
     requestFailure(data.status, data.message, showAlert); 
    } else if (data.status == '') { 
     requestFailure(data.status, data.message, showAlert); 
    } 
} 

/* AJAX Request Function */ 

function serverRequest(requestDATA, successCallback, errorCallback, hideIndicator, showAlert){ 
var METHOD = "POST"; 
    var serverURL = 'http://localhost/istudy/server.php'; //Path to Server.php 
    var DATA_TYPE = 'json'; 
    var TIMEOUT = 20000; 
    console.log(requestDATA); 
    $$.ajax({ 
     url: serverURL, 
     data: requestDATA, 
     dataType: DATA_TYPE, 
     type: METHOD, 
     timeout: TIMEOUT, 
     success: function(data){ 
      successCallback(data, hideIndicator, showAlert); 
     }, 
     error: function(a, b, c){ 
      errorCallback(a, b, c, hideIndicator, showAlert); 
     } 
    }); 
} 

/* Function to handle request error */ 

function responseError(xhr, textStatus, errorThrown, hideIndicator, showAlert){ 
    console.log(xhr); 
    console.log(textStatus); 
    console.log(errorThrown); 
    var error_message = ""; 
    switch(textStatus){ 
     case 'error': 
     error_message = "Please check your internet connection and try again."; 
     break; 
     case 'parsererror': 
     error_message = "Internal error occureed. Report application administrator about this error."; 
     break; 
     case 'timeout': 
     error_message = "Slow internet may be. Pull down to refresh page."; 
     break; 
     case 'abort': 
     error_message = "The request was aborted."; 
     break; 
     default: 
     error_message = "Cannot reach server at this time. You can report this error."; 
     break; 
    } 
    if(hideIndicator){ 
     myApp.hideIndicator(); 
    } 

    if(showAlert){ 
     myApp.alert(error_message, "Oh Snap! :(", null); 
    } 
} 

/* Request With Server Fail or Error or Others */ 

function requestFailure(status, message, showAlert){ 
    if(showAlert){ 
     if(status == 'error'){ 
      myApp.alert(message, 'No Data Available!', null); 
     }else if(status == ''){ 
      myApp.alert(message, 'Request Failure!', null); 
     }else{ 
      if(showAlert){ 
       myApp.alert("Application was not ready to serve this request at this time.", 'Unknown Response', null); 
      } 
     } 
    } 
} 

すべて正常です。クエリはresponseTextの結果も取得しますが、私はparseerrorを取得します。どのような問題が起こる可能性がありますか?

下記の添付画像をご覧ください。

enter image description here

何の問題になることができますか?

+0

いいえどこで使うのですか? –

+0

私の機能のドームは正常に動作しています。いくつか私にこのエラーを与える。なぜ私は理解できないのですか?いくつかのオブジェクトはイメージに示されているように返され、いくつかはparseerrorを表示します。 –

+0

'var_dump($ response_array) 'とは何ですか? – Rayon

答えて

0

DATA_TYPEjsonと定義しているため、要求を実行した後に来るデータはすでにarrayに変換されています。だからこそあなたが得ているのですparse error

関連する問題