This AJAX request returns 'success' if the output in PHP is copied and pasted JSON, but 'fail' if it's generated by the file. Have a look at the comments in api.php below to see what I mean.
$.ajax({
url: 'api.php',
dataType: 'JSON',
success: function() {
alert('success');
},
error: function() {
alert('fail');
}
});
api.php:
<?php
header('Access-Control-Allow-Origin: *');
$jsonurl = "http://findyourmp.parliament.uk/api/search?q=st+pancras&f=js";
$json = file_get_contents($jsonurl);
// THIS DOESN'T WORK (RETURNS 'FAIL')
print_r($json);
?>
<!-- THIS WORKS (RETURNS 'SUCCESS') -->
{"results": { "constituencies": [{"constituency_name": "Holborn and St Pancras", "member_name": "Frank Dobson", "member_party": "Labour", "member_biography_url": "http://www.parliament.uk/biographies/commons/Frank-Dobson/180", "member_website": "", "uri": "http://findyourmp.parliament.uk/constituencies/holborn-and-st-pancras.json" } ], "members": [] }}