problem with PHP, JSON, and ListGrid

I can get the grid to work if I have it pull a JSON file that I create with PHP, but if I return the results straight to the ListGrid from PHP, the “getting results from server” message never goes away on the page.

Here is my HTML page:

<html>
<body>

<SCRIPT>var isomorphicDir='smartClient/isomorphic/';</SCRIPT>
<SCRIPT SRC='smartClient/isomorphic/system/modules/ISC_Core.js'></SCRIPT>
<SCRIPT SRC='smartClient/isomorphic/system/modules/ISC_Foundation.js'></SCRIPT>
<SCRIPT SRC='smartClient/isomorphic/system/modules/ISC_Containers.js'></SCRIPT>
<SCRIPT SRC='smartClient/isomorphic/system/modules/ISC_Grids.js'></SCRIPT>
<SCRIPT SRC='smartClient/isomorphic/system/modules/ISC_Forms.js'></SCRIPT>
<SCRIPT SRC='smartClient/isomorphic/system/modules/ISC_DataBinding.js'></SCRIPT>
<SCRIPT SRC='smartClient/isomorphic/skins/SmartClient/load_skin.js'></SCRIPT>
<script>

	isc.XJSONDataSource.create({
	    ID:'employees',
	    dataFormat:'json',
	    dataURL:'http://localhost/hb/sugar/sc_test.php?json=yes',
	    fields:[
	{title:'First Name', name:'first_name'},{title:'Last Name', name:'last_name'}
	    ]
	});
	
		isc.ListGrid.create({
			ID: 'myGrid',
		    width: '',
		    height: '',
		    dataSource: 'employees',
		    autoDraw:true,
		    alternateRecordStyles:true,
		    showAllRecords:true,
		    position:'relative',
		    autoFetchData: true
		});
	</script>

</body>
</html>

Here is the PHP page that creates the JSON data (also creates the HTML):

<?
require_once('include/inc/bh_db.inc.php');
require_once('smart_client.inc.php');

extract($_REQUEST);

if($json=='yes')
{
	// return the data in json format
	$sql = "select first_name,last_name from users order by last_name,first_name limit 5";
	$rs  = dbCall($sql,false);
	$c = rs2json($rs);
	//string2file('employees.js',$c,'w');
	print $c;
	exit();
}

print "<html>
<body>
";

print scIncludes();

$fields = array("First Name"=>"first_name","Last Name"=>"last_name");

print "<script>\n";
print scCreateDataSource('employees',"http://localhost/hb/sugar/sc_test.php?json=yes",$fields);
print scCreateListGrid('myGrid','','','employees','true');
print "</script>\n";

print "
</body>
</html>
";
?>

Here is the JSON output from the script in the dataURL:

[ {first_name:'Paul', last_name:'Abfalter'}, {first_name:'Roy', last_name:'Abshier'}, {first_name:'Roy', last_name:'Acrea'}, {first_name:'John', last_name:'Adams'}, {first_name:'Nathan', last_name:'Adamson'} ]

Can someone please tell me what I am doing wrong?

Thanks,
Scott Hathaway

Hi Scott,

This one’s easy - just switch from “XJSONDataSource” to an ordinary DataSource. “XJSONDataSource” is designed for doing cross-domain calls (eg a page loaded from smartclient.com calling a page at yahoo.com) and requires the server to write out a callback function in addition to the data. So long as you are loading data from a URL on the same server from which you load the page itself (the “origin server”), you don’t need to use an XJSONDataSource or have the server provide callback functionality.

To ensure that the browser understands you are contacting the origin server, you should omit the hostname from the dataURL (so just /hb/sugar/sc_test.php).

If you do need to cross-domain calls, the Yahoo developer network has a good explanation of how JSON callbacks work.

that worked!

Thank you for the quick reply!

:slight_smile:
Scott