Problem with cross-site Ajax calls and PHP


The following example is demonstrating Ajax call made to the weather service.

As long as service calls:
Ajax is returning success.

Sample Ajax request is presented below:

var jqxhr = $.ajax({

url: “”,

//url: “”,

crossDomain: true,

dataType: “json”,


.done(function (data) {


elements =;


$.each(elements, function (index, element) {





.fail(function (jqXHR) {


if (jqXHR.status === 0) {

alert(‘Not connect.\n Verify Network.’);

} else
if (jqXHR.status == 404) {

alert(‘Requested page not found. [404]’);

} else
if (jqXHR.status == 500) {

alert(‘Internal Server Error [500].’);

} else
if (exception === ‘parsererror’) {

alert(‘Requested JSON parse failed.’);

} else
if (exception === ‘timeout’) {

alert(‘Time out error.’);

} else
if (exception === ‘abort’) {

alert(‘Ajax request aborted.’);

} else {

alert(‘Uncaught Error.\n’ + jqXHR.responseText);






The problem occurred when I created PHP page, wrapping up an original Ajax call; my Ajax responded with the error, although both requests, from the original site and from my PHP page responded with the identical content:

Original site:

New PHP page:

Here is the source code of my PHP page:



$jsonContent = file_get_contents(“;);

$jsonContent = str_replace(“\n”,”, $jsonContent);

header(‘Content-Type: application/json’);

echo stripslashes(($jsonContent));?>



The problem was related to the access control mechanism, for cross-site HTTP requests. The problem was solved by adding yellow-marked header in the code below:



$jsonContent = file_get_contents(“;);

$jsonContent = str_replace(“\n”,”, $jsonContent);

header(“Access-Control-Allow-Origin: *”);

header(‘Content-Type: application/json’);

echo stripslashes(($jsonContent));?>


Additional resources

Enable root password in Linux

Here is simple instruction how to enable root account on Raspbian:

pi@raspbmc:/$ sudo passwd root

Enter new UNIX password:

Retype new UNIX password:

passwd: password updated successfully

pi@raspbmc:/$ su