Serving JSON

DrJohnZoidberg

Honorary Master
Joined
Jul 24, 2006
Messages
27,997
Reaction score
7,454
Location
Table View
Hey all codemasters!

I am struggling to get my head around something so trivial.

I have an already formatted JSON file, I want to make this available as a service for our web developer to be able to make ajax calls to. It is giving him cross domain errors though when he tries to call it as a flat file from my server.

What is the easiest way to serve the json so he can access it without issues?

Sorry, I am very clueless when it comes to this stuff and don't have a lot of time to research this stuff.
 
Will something like this work?

Code:
<?php

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$param1 = "";
$param2 = "";

if (isset($_GET['param1'])) {
 $param1 = $_GET['param1'];
} else
$param1 = "";

if (isset($_GET['param2'])) {
 $param2 = $_GET['param2'];
 $file_name = "/var/www/api/histories/test-" . $param1 . "-" . $param2 . ".json";
 $json_file_contents = file_get_contents($file_name);
 echo $json_file_contents;
} else
$param2 = "";


?>

Can I just echo the data like this? I know it's very crude to call the files like this, I have everything in a mongo database but implementing all the libraries required on his side is just a mission right now.
 
Top
Sign up to the MyBroadband newsletter
X