Tuesday, February 26, 2008

ScriptTagProxy invalid label solution

ExtJS.com
If you have problems when you want to fill ExtJS grid to get data from remote domain may be you will get error that says 'invalid label' because it should be a javascript content not JSON content and the script should call the callback function was declare in the URL.

Based on Ext ScriptTagProxy documentation here the PHP code to fix that error :

<php
require_once('../lib/json/json.php');
$json = new Services_JSON();

$scriptTag = false;
$cb = $_GET['callback'];

if (!empty($cb)) {
$scriptTag = true;
header("Content-type: text/javascript");
} else {
header("Content-type: application/x-json");
}

if ($scriptTag) echo $cb . "(";

echo $json->encode(
Array(
'totalCount' => $res->numRows(),
'dt' => getData($_GET['start'])
)
);

if ($scriptTag) echo ");";
?>
First we check callback variable if it set we set content-type for browser to javascript and add callback function to JSON data retrieved.

Ok. Hope it help you
Agus Sudarmanto

3 comments:

Nyxdell said...

Gracias socio. me salvaste el dia ya estaba loco con este error y no entendia bien la explicacion de la api, muchas gracias.

Thank you very much

Unknown said...

$jsonData = json_encode($data);
$jsonData= "({\"success\":true,\"totalCount\": \"".count($data)."\",\"records\":".$jsonData."})";

if (isset($_REQUEST['callback'])) {
header('Content-Type: text/javascript');
echo $_REQUEST['callback']. '(' . $jsonData . ');';
} else {
header('Content-Type: application/x-json');
echo $jsonData;
}

Unknown said...

Assuming you are using php:
To avoid cant modify header error, make sure no blank line before <?php