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()];
}
}
你能告訴我你的Ajax代碼? – rahul
請添加包含AJAX調用的jquery代碼。 –
@Plum我添加了相關的jquery函數。我將添加PHP方法也... – Eamonn