Ajax调用不等待服务器响应

时间:2016-02-10 12:55:37

标签: php jquery ajax wamp

我在Windows10机器上使用WAMP v.2.5。我的项目是一个运行MySQL DB的PHP项目。它包括许多AJAX调用,它们工作正常。我有一个特定的电话,但它给了我一个意外的输入结束'错误。

调用是从View调用的,是一个全局的ajax处理程序PHP脚本,它将请求转发给Controller,然后Controller要求Model获取响应。正在解雇相应的Model方法。该方法包含错误检查,并将为空结果抛出异常。内部数据库查询有效,并在控制台中使用时返回结果。然而,在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()];
    }
}

1 个答案:

答案 0 :(得分:0)

找到了罪魁祸首:

我必须将我的Apache max_input_vars更新到更高的限制,以允许返回的实际 返回的各个参数的数量。邮政大小不是问题。