我遇到了 JSONP 请求未完整返回给客户端的问题(Sencha Touch 1.1)。我将 PHP 5.2.5 与 Apache2 一起使用。该请求是通过表达式引擎模板构建的,并使用标头“Content-type: text/javascript; charset=utf-8”发送。打开输出缓冲。我必须在它发送正确的标题之前手动刷新,否则它将作为“text/html”发送。
我已经在本地测试了代码,它确实构建并正确返回了请求。在 prod 和 dev 上,它将删除请求的内容。例子:
stcCallback1001(,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]});
我认为这是一个环境或安全策略问题,因为它确实在我使用 XAMPP Apache2/PHP 5.3.8 的机器上正常工作。下面的一段代码:
$stores = array();
foreach ($results as $key => $value)
{
$week = ($value['field_id_51'] == 'Y') ? '24hrs' : $value['field_id_49'] . '-' . $value['field_id_50'];
$sat = ($value['field_id_54'] == 'Y') ? '24hrs' : $value['field_id_52'] . '-' . $value['field_id_53'];
$sun = ($value['field_id_57'] == 'Y') ? '24hrs' : $value['field_id_55'] . '-' . $value['field_id_56'];
$dist = distance($lat, $lon, $value['field_id_58'], $value['field_id_59']);
if ($dist <= $rad)
{
$stores[] = array(
"store_id" => (string)$value['title'],
"street" => (string)$value['field_id_43'],
"city" => (string)$value['field_id_44'],
"state" => (string)$value['field_id_45'],
"zip" => (string)$value['field_id_46'],
"phone" => (string)$value['field_id_41'],
"fax" => (string)$value['field_id_42'],
"pharm" => (string)$value['field_id_47'],
"pharm_2" => (string)$value['field_id_48'],
"week" => (string)$week,
"sat" => (string)$sat,
"sun" => (string)$sun,
"lat" => (string)$value['field_id_58'],
"lon" => (string)$value['field_id_59'],
"distance" => (string)$dist,
);
}
}
$totalResults = count($stores);
$stores = ($totalResults > 0) ? subval_sort($stores, 'distance') : '';
$push = array(
"latitude" => $lat,
"longitude" => $lon,
"radius" => $rad,
"numResults" => $totalResults,
"stores" => $stores
);
$callback = (isset($_GET['callback'])) ? ($_GET['callback']) : null;
if($callback) {
header('Content-type: text/javascript; charset=utf-8');
echo $callback . '(' . json_encode($push) . ');';
flush();
}
else {
header('Content-type: application/x-json; charset=utf-8');
echo json_encode($push);
flush();
}
该代码之前在 prod 上工作而没有刷新。我以前从来没有遇到过问题。我已验证在服务器上正确创建了 JSON。它似乎在运输过程中被修改。寻找关于它可能是什么和可能的解决方案的想法。