[英]Ajax call not waiting for server response
我使用的是Windows10的機器上WAMP v.2.5。 我的項目是一個在MySQL數據庫上運行的PHP項目。 它包含大量的AJAX調用,效果很好。 我有一個特定的電話,但是這給了我“輸入意外結束”錯誤。
該呼叫從一個視圖中進行,是針對全球的AJAX處理PHP腳本,將請求轉發給控制器,然后請求的響應模型。 觸發了適當的Model方法。 該方法中包含錯誤檢查,並會拋出異常的一個空的結果。 其中的DB查詢有效,並在控制台中使用時返回結果。 但是,十分之九的次數,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()];
}
}
找到了罪魁禍首:
我不得不更新我的Apache max_input_vars到更高的限制,允許返回實際返回各個參數的數量。 帖子大小不是問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.