Hi,
This is the first time I have used jQueries $.getJSON trying to cross-domains and get some JSON formatted data.
I have created a php JSON service:
<?php
class listings {
/**
* PDO database object
* @access private
* @var object
*/
protected $pdo;
/**
* The id which identifies this user
* @access private
* @protected int
*/
protected $company_id;
/**
* The username which is drawn from the stored username in the current session
* @access private
* @protected int
*/
function __construct(DbFactory $pdo) {
$this->pdo=$pdo;
}
/**
* Determines the user's id from the login session variable
* @return void
* @access private
*/
function populate($company_id) {
$sql="SELECT
co2ls.company_id
, cf.is_bold
, cf.is_thin_outline
, cf.is_orange_highlight
, ls.title
, ls.listing_id
, ls.listing
FROM company2classified as co2ls
INNER
JOIN classifieds as ls
ON (ls.listing_id = co2ls.listing_id)
INNER
JOIN configuration as cf
ON (ls.listing_id = cf.listing_id)
WHERE
co2ls.company_id = $company_id;";
$this->stmt = $this->pdo->db->prepare($sql);
$classifieds = array();
$values = array();
try {
if ($this->stmt->execute()) {
$values = array();
$listings = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($listings as $key => $value) {
$values[$key] = $value;
}
}
$classifieds['listings'] = $values;
return ($classifieds);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\
";
}
}
}
?>
and the output php script:
<?php
require_once('Listings.php');
require_once('DbFactory.php');
$pdo = new DbFactory();
$Obj_Listings = new Listings($pdo);
$json = json_encode($Obj_Listings->populate($_GET['company']));
echo $json;
?>
This outputs the following RAW JSON output
{"listings":[{"company_id":"101","is_bold":"0","is_thin_outline":"1","is_orange_highlight":"1","title":"Registered Lab Puppies For Sale","listing_id":"1000002","listing":"Lab Puppies for sale. Registered. Excellent breeding stock. Call to arrange a visit. Prices start at $700."},{"company_id":"101","is_bold":"0","is_thin_outline":"0","is_orange_highlight":"0","title":"Rare Lizard","listing_id":"1000003","listing":"Rare Lizard for sale - illegally imported into the country (keep it quite) $10000"}]}
formated for easier reading
{
"listings":[
{
"company_id":"101"
,"is_bold":"0"
,"is_thin_outline":"1"
,"is_orange_highlight":"1"
,"title":"Registered Lab Puppies For Sale"
,"listing_id":"1000002"
,"listing":"Lab Puppies for sale. Registered. Excellent breeding stock.
Call to arrange a visit. Prices start at $700."
}
,{
"company_id":"101"
,"is_bold":"0"
,"is_thin_outline":"0"
,"is_orange_highlight":"0"
,"title":"Rare Lizard"
,"listing_id":"1000003"
,"listing":"Rare Lizard for sale - illegally imported into the country (keep it quite) $10000"
}]
}
The html on the other domain that has the JSONP jQuery is:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Classified Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script src="http://www.google.com/jsapi?key=ABQIAAAABZGjjDpjmjbzLaNWBpdrWhRYfwzT-VuwidSQZM_JU-MUSrbEShR1efDdxuqpWPsjsfs1V_59FUTrxg" type="text/javascript"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<div id='content'>
</div>
<script type="text/javascript">
$(document).ready(function(){
var url="http://www.liviam.ca/Ice_Rink/index.php?company=101&callback=?";
$.getJSON(url,function(json){
$.each(json.listings,function(i,listing){
$("#content").append(
'<div class="listing">'+
'<h1>'+listing.title+'</h1>'+
'<ul>'+
'<li>'+listing.company_id+'</li>'+
'<li>'+listing.is_bold+'</li>'+
'<li>'+listing.is_thin_outline+'</li>'+
'<li>'+listing.is_orange_highlight+'</li>'+
'<li>'+listing.listing+'</li>'+
'</ul'+
'</div>'
);
});
});
});
</script>
</body>
</html>
You can see that in the url I am using the callback=?
var url=“http://www.liviam.ca/Ice_Rink/index.php?company=101&callback=?”;
As I am very new to jQuery’s $.getJSON() I don’t know what I am doing wrong as there appears to be no data returned. I have gone step by step using Firebug and it seems to die on the $.each function.
Here are the urls for your reference:
http://www.liviam.ca/Ice_Rink/index.php?company=101&callback=?
and
Can you see why I either don’t have any data or am not referencing it correctly?
Your thoughts are appreciated.
Regards,
Steve