I'm having an issue with a JSONP request not returning intact to the client (Sencha Touch 1.1). I'm using PHP 5.2.5 with Apache2. The request is built through an Expression Engine template, and sent using header "Content-type: text/javascript; charset=utf-8". Output buffering is turned on. I have to manually flush before it sends the correct header, or else it will send it as "text/html".
I've tested the code locally and it does build and return the request correctly. On prod and dev, it will strip the content of the request. Example:
stcCallback1001(,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]});
I'm thinking it is an environment or security policy problem since it does work correctly on my machine using XAMPP Apache2/PHP 5.3.8. A snippet of code below:
$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();
}
The code worked on prod before without flushing. I've never had a problem before. I've validated the JSON is created on the server correctly. It seems to be modified during transport. Looking for ideas on what it could be and possible solution.
Apparently the PHP script was having a memory issue which resulted in it not creating the JSON prior to output. I'm not sure how this issue started as I'm not responsible for maintaining the server environment. To fix this issue, I added a:
die();
At the end of the script to cancel execution. I also took out the call to flush()
. This resulted in the correct output expected.