2016-11-23 116 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