Web Service using JSONP from External Domain – PHP – JQuery

You may want to look at this previous post where I setup a PHP Webservice that uses JSON PDO – Accessed by JQuery AJAX

OK, so this code will use php to generate the JSON code that will get delivered over the internet to the JSONP (JSON Padding) client on an external domain. This allows your webservice to be available too whoever wants to call it.

Here is the PHP Code: (Not all of it, but enough to get the idea)

private function GetTriviaTag($row) {
	$t = new Tag();
	$t->id = $row['id'];
	$t->triviaid = $row['triviaid'];
	$t->tagid = $row['tagid'];
	return $t;
public function GetWhere($where) {
	$sql = "SELECT * FROM trivia_tags WHERE {$where}";
	foreach ($this->pdo->query($sql) as $row)
		$retArray[] = $this->GetTriviaTag($row);
	return $retArray;
$where = " (1 = 1)";  // THIS WILL SELECT ALL
$tarray = GetWhere($where);
header('Content-type: application/json');
echo "(" . json_encode($tarray) . ")";

And now, to access it from javascript in an external domain (non – SERVER_domain.com ):

		<script src="http://code.jquery.com/jquery-latest.js"></script>
		<div id="triviaQuestions"> loading... </div>
			$(function() {
				// THE 'callback=?' in this request is IMPORTANT FOR EXTERNAL DOMAIN ACCESS!!!
				$.getJSON('http://SERVER_domain.com/webservice.php&callback=?', function(data) {
					var items = [];
					var htmlResult = "";
					$.each(data, function(key, val) {
						htmlResult += "<br />Q: "+ val.id +", '"+ val.question +"";

CWS – Chord Web Service


I am creating a web service for musicians that will allow them to reference an abundance of musical references on the fly using AJAX.

Firstly, it will deliver chords and fingerings of chords for guitars to the users websites (think Google Maps).

My goal is to enhance the web.

Here is the site: http://cws.danfolkes.com

It’s still in development, but it’s getting close to launch.

Let me know what you think (only if it’s nice) 😀