2016-11-23 157 views
0

我正在創建一個API。這是我的PHP函數。通過結構函數獲取用於AJAX調用的parseerror

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_TYPE定義爲json這意味着執行請求後即將發生的數據已轉換爲array。這就是爲什麼你越來越parse error