I have to make a request from javascript that is on server A, to a php file that is on server B. I can access both servers.
But something is getting wrong. I always get ready state 0, status 0.
This is the latest thing I tried: Please advice what I'm doing wrong. thanks.
Server A:
$.ajax({
type: "GET",
url: 'http://server_B/request.php',
data: form_data,
dataType: 'json',
success: function (resp) {
alert("Successful");
console.log("Response completed");
console.log("resp is" + resp);
},
error: function (xhr, error) {
console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
console.log("responseText: " + xhr.responseText);
alert("Error occurred.");
}
});
Server B: request.php
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
# do the work and save it on $result array.
print json_encode($result,true);
?>