PDA

View Full Version : How-To read JSONStore from remote domain



amegahed
23 Sep 2009, 10:53 AM
Been trying to do this all day yesterday, until finally got it to work, so i thought i'll put it up here for anyone looking for the same thing. I wanted to fetch data from a remote MySQL DB located on a remote domain (other that my local machine). All the documents and API referred to using ScriptTagProxy instead of HttpProxy to connect to the remote domain, so after getting bits from here and there, this is the solution that finally worked for me:

On the remote server, this the php page that returns my data:


<?php
$callback = $_REQUEST['callback'];
include 'opendb.php';

$query = "select * from map";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
$myArray[] = array( "cityid"=>$row['cityid'],
"districtid"=>$row['districtid'],
"mapname"=>$row['mapname']
);

}

$return = array( 'total' => count($myArray), 'results' => $myArray );
$returnJSON = json_encode($return);
echo $callback.'('.$returnJSON.')';

?>


on my local side, this is the js page:


// dsMapRemote dataStore
var dsMapRemote = new Ext.data.JsonStore({
proxy : new Ext.data.ScriptTagProxy ({
url : 'http://www.xxxxxxxxxx.com/php/xxxxx.php'
}),
totalProperty : 'total',
root : 'results',
fields : [ 'cityid', 'districtid', 'mapname']
});

.....

dsMapRemote.load();


and thats it :)

Edit: Sorry for posting in the wrong place .. now I know where to post such articles :)