调用Ajax获取Google Calendar数据时出现ERR_INCOMPLETE_CHUNKED_ENCODING等错误求助
问题排查与解决方案
1. 空响应(net::ERR_EMPTY_RESPONSE)的核心修复
看actionSync方法,当$hasRecord == NULL时,$list变量未定义就直接传入renderPartial,会触发PHP未定义变量警告,中断输出流程,导致部分用户拿到空响应。
修复代码:
public function actionSync(){ $list = []; // 提前初始化空数组,避免未定义错误 $hasRecord = EmpGoogleCalendar::model()->find('id=:userid', array(':userid' => $_POST['userid'])); if ($hasRecord != NULL){ $google_calendar = new GoogleCalendar(); $client = $google_calendar->Authenticate($_POST['userid']); $list = $google_calendar->getGoogleCalandarList($client); } $this->renderPartial('view',array('list'=>$list)); }
注:顺便修复了SQL注入风险,改用参数绑定查询数据库
2. 分块编码错误(net::ERR_INCOMPLETE_CHUNKED_ENCODING)的排查方向
(1)捕获API请求异常
调用Google Calendar API时,网络波动、权限失效等问题会抛出异常,中断输出。必须添加异常捕获:
public function getGoogleCalandarList($client){ $service = new Google_Service_Calendar($client); $dropdownarray = array(); try { $calendarList = $service->calendarList->listCalendarList(); while(true) { foreach ($calendarList->getItems() as $calendarListEntry) { $dropdownarray[] = array( 'id'=>$calendarListEntry->id, 'summary'=>$calendarListEntry->getSummary(), ); } $pageToken = $calendarList->getNextPageToken(); if ($pageToken) { $optParams = array('pageToken' => $pageToken); $calendarList = $service->calendarList->listCalendarList($optParams); } else { break; } } } catch (Exception $e) { error_log('Google Calendar API Error: ' . $e->getMessage()); // 记录错误日志 return $dropdownarray; // 返回空数组避免输出中断 } return $dropdownarray; }
(2)调整PHP输出缓冲
服务器压缩或缓冲设置不当会导致输出截断,可在actionSync开头强制清理缓冲:
public function actionSync(){ ob_clean(); // 清空缓冲区残留内容 ob_end_flush(); // 关闭输出缓冲 $list = []; // ... 后续原有代码 }
(3)完善Ajax错误处理
当前Ajax仅监听success,后端返回错误状态码时无提示,补充错误处理:
$.ajax({ url: '/index.php/Syn/getCalandars', data: { userid: 1 }, type: 'POST', success: function(res) { $("#ajax_load_here").html(res); }, error: function(xhr, status, error) { $("#ajax_load_here").html('加载日历列表失败,请稍后重试'); console.log('请求错误:', status, error); } });
(4)升级Google API客户端
你使用的google-api-php-client-master是旧版分支,存在已知的请求处理bug,建议升级到最新稳定版,同时在客户端设置超时:
// 在Authenticate方法中添加超时设置 $client->setTimeout(15); // 15秒超时,避免请求挂起
3. 额外排查步骤
- 查看服务器日志(Apache/Nginx的
error.log、PHP的error_log),确认是否有致命错误或警告中断输出; - 检查部分异常用户的网络环境,是否存在Google API访问受限情况;
内容的提问来源于stack exchange,提问作者Ramesh Madusanka
相关产品推荐
相关产品推荐

