2016-02-10 71 views
0

我使用的是Windows10的机器上WAMP v.2.5。我的项目是一个运行MySQL数据库的PHP项目。它包含许多AJAX调用,这些调用很好。我有一个具体的电话,但是这给我一个'意外的输入结束'错误。Ajax调用不等待服务器响应

的呼叫从一个视图中进行,是针对全球的AJAX处理PHP脚本,将请求转发给控制器,然后请求的响应模型。适当的模型方法正在被解雇。该方法包含错误检查,并将为空结果抛出异常。数据库中的数据库查询有效,并在控制台中使用时返回结果。然而,10次中有9次,ajax fn会在没有接收/读取查询结果的情况下完成,从而产生上述错误。有时它会正常工作。

当放置一个活的服务器上,一切正常,因为它应该。就好像脚本在本地计算机上运行得太快,以等待数据库响应或抛出任何异常。

谁能告诉我如何正确地检验所发生的事情,或有解决上述问题?

编辑:
小道受影响的代码:

$(document).ready(function() 
{ 
    //some code 
    updateFilteredScheduleList(); 
    //some code 
}); 

function updateFilteredScheduleList() 
{ 
    var opts = $.extend(true, {}, dialogOptions); 
    getFilteredScheduleResults() 
     .done(function(returnedData) 
     { 
      var returnedDataObj = parseAjaxJSONResponse(returnedData); 

      if(returnedDataObj.hasOwnProperty('success')) 
       buildScheduleList(returnedDataObj.response); 
     }) 
     .error(function(xhr, options, error) 
     { 
      opts.message = error; 
      displayDialog(opts); 
      return false; 
     }); 
} 

function getFilteredScheduleResults() 
{ 
    var values = getFilterValues(); 
    values.controller = 'WSVisits'; 
    values.method = 'getFilteredScheduleResults'; 

    console.log(values); 

    return $.ajax({ 
     type: 'post', 
     cache: false, 
     data: values, 
     url: controllersAjaxPath 
    }); 
} 

function getFilterValues() 
{ 
    var values = {}; 
    //get values of view filters 
    return values; 
} 

function parseAjaxJSONResponse(data) 
{ 
    var opts = $.extend(true, {}, dialogOptions); 
    try 
    { 
     var tmp = JSON.parse(data); 
     if(tmp.hasOwnProperty('error')) 
     { 
      opts.message = tmp.error; 
      displayDialog(opts); 
      return false; 
     } 

     return tmp; 
    } 
    catch(e) 
    { 
     opts.message = e.message; 
     displayDialog(opts); 
     return false; 
    } 
} 

PHP方法(稍微改动过):

function getFilteredScheduleResults($args = null) 
{ 
    $id = intval($args['MyID']); 
    $region_id = (!$id) ? (intval($args['RegionID']) > 0) ? intval($args['RegionID']) : 0 : 0; 
    $county_id = (!$id) ? (intval($args['CountyID']) > 0) ? intval($args['CountyID']) : 0 : 0; 
    $language_id = (!$id) ? (intval($args['LanguageID']) > 0) ? intval($args['LanguageID']) : 0 : 0; 
    $center_id = (!$id) ? (intval($args['CenterID']) > 0) ? intval($args['CenterID']) : 0 : 0; 
    $type_id = (!$id) ? (intval($args['TypeID']) > 0) ? intval($args['TypeID']) : 0 : 0; 
    $support_type_id = (!$id) ? (intval($args['SupportTypeID']) > 0) ? intval($args['SupportTypeID']) : 0 : 0; 

    $address_token = (!$id) ? (trim($args['AddressContains']) !== '') ? trim($args['AddressContains']) : null : null; 

    $purpose_id = (intval($args['PurposeID']) > 0) ? intval($args['PurposeID']) : 0; 
    $associate_id = (intval($args['AssociateID']) > 0) ? intval($args['AssociateID']) : 0; 
    if(!empty($args['From'])) 
    { 
     $from_obj = DateTime::createFromFormat('d/m/Y', $args['From']); 
     $args['From'] = (!$from_obj) ? null : $from_obj->format('Y-m-d'); 
    } 
    if(!empty($args['To'])) 
    { 
     $to_obj = DateTime::createFromFormat('d/m/Y', $args['To']); 
     $args['To'] = (!$to_obj) ? null : $to_obj->format('Y-m-d'); 
    } 


    $sql = " /*query*/ WHERE 1 "; 

    if($id) 
     $sql.= " AND (s.MyID = :MyID) "; 
    else 
    { 
     if($region_id) 
      $sql.= " AND (RegionID = :RegionID) "; 
     if($county_id) 
      $sql.= " AND (CountyID = :CountyID) "; 
     if($language_id) 
      $sql.= " AND (LanguageID = :LanguageID) "; 
     if($center_id) 
      $sql.= " AND (CenterID = :CenterID) "; 
     if($type_id) 
      $sql.= " AND (s.TypeID = :TypeID) "; 
     if($support_type_id) 
      $sql.= " AND (SupportTypeID = :SupportTypeID) ";"; 
     if(!is_null($address_token)) 
      $sql.= " AND (UPPER(CONCAT_WS(' ', Add1, Add2, Add3, CityTown)) LIKE UPPER(:AddressToken)) "; 
    } 

    $sql.= " GROUP BY s.MyID ORDER BY MyName ASC "; 

    $db = new Database(); 
    try 
    { 
     $db->query($sql); 
     if($id) 
      $db->bind(':MyID', $id); 
     else 
     { 
      if($region_id) 
       $db->bind(':RegionID', $region_id); 
      if($county_id) 
       $db->bind(':CountyID', $county_id); 
      if($language_id) 
       $db->bind(':LanguageID', $language_id); 
      if($center_id) 
       $db->bind(':CenterID', $center_id); 
      if($type_id) 
       $db->bind(':TypeID', $type_id); 
      if($support_type_id) 
       $db->bind(':SupportTypeID', $support_type_id); 
      if(!is_null($address_token)) 
       $db->bind(':AddressToken', '%' . $address_token . '%'); 
     } 
     $db->execute(); 
     $tmp = $db->fetchAllAssoc(); 

     $get_assignments_only = (!empty($args['AssignmentsOnly'])); 
     $returned = []; 

     $sql = " SELECT VisitID FROM visits_ws WHERE MyID = :MyID "; 
     if($purpose_id) 
      $sql.= " AND (VisitPurposeID = :Purpose) "; 
     if($associate_id) 
      $sql.= " AND ((Associate1ID = :AssociateID) OR (Associate2ID = :AssociateID) OR (Associate3ID = :AssociateID) OR (Associate4ID = :AssociateID)) "; 
     if(!empty($args['From'])) 
      $sql.= " AND (VisitDate >= :From) "; 
     if(!empty($args['To'])) 
      $sql.= " AND (VisitDate <= :To) "; 
     $db->query($sql); 

     foreach($tmp as $i => $t) 
     { 
      $db->bind(':MyID', $t['MyID']); 
      if($purpose_id) 
       $db->bind(':Purpose', $purpose_id); 
      if($associate_id) 
       $db->bind(':AssociateID', $associate_id); 
      if(!empty($args['From'])) 
       $db->bind(':From', $args['From']); 
      if(!empty($args['To'])) 
       $db->bind(':To', $args['To']); 
      $db->execute(); 
      $visits = $db->fetchAllAssoc(); 

      if(($get_assignments_only) && (empty($visits))) 
       continue; 

      if((($purpose_id) || ($associate_id) || (!empty($args['From'])) || (!empty($args['To']))) && (empty($visits))) 
       continue; 

      $tmp[$i]['HasVisits'] = (empty($visits)) ? 0 : 1; 
      $tmp = $schools[$i]; 
      unset($tmp['Name']); 
      $schools[$i]['Address'] = build_address($tmp); 

      unset($schools[$i]['Add1']); 
      unset($schools[$i]['Add2']); 
      unset($schools[$i]['Add3']); 
      unset($schools[$i]['CityTown']); 
      unset($schools[$i]['CityPostCode']); 
      unset($schools[$i]['Name']); 
      unset($schools[$i]['LanguageID']); 

      unset($schools[$i]['PrincipalID']); 
      unset($schools[$i]['ContactID']); 
      unset($schools[$i]['TypeID']); 
      unset($schools[$i]['CenterID']); 
      unset($schools[$i]['SupportTypeID']); 
      unset($schools[$i]['CountyID']); 
      unset($schools[$i]['AreaCodeID']); 
      unset($schools[$i]['NetworkCodeID']); 
      unset($schools[$i]['RegionID']); 

      $returned[] = $tmp[$i]; 
     } 

     return ['jct_success'=>'ok', 'response'=>$returned]; 
    } 
    catch(PDOException $e) 
    { 
     return ['jct_error'=>$e->getMessage()]; 
    } 
} 
+4

你能告诉我你的Ajax代码? – rahul

+0

请添加包含AJAX调用的jquery代码。 –

+0

@Plum我添加了相关的jquery函数。我将添加PHP方法也... – Eamonn

回答

0

找到了罪魁祸首:

我不得不更新我的Apache max_input_vars到更高的限制,以允许返回到个人参数的数目实际上是返回。邮政大小不是问题。