I know some of you have asked this question but my problem is different. When both the JS and php files are in the same domain, they work like a charm, but when I put the PHP file on a remote server and make the call to that server, I don't get any result back. Below is my code. Please let me know what I'm doing wrong. Do I need to edit my htaccess file?
-----JS-----
$("document").ready(
function(){
$(".js-ajax-php-json").submit(
function(){
var data = {
"action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
jQuery.support.cors = true;
$(".the-return").html('<img src="loading.gif" />');
$.ajax({
type: "POST",
dataType: "json",
url: "http://********/response.php",
data: data,
success: function(data) {
$(".the-return").html(
"Favorite beverage: " + data["favorite_beverage"] + "<br />Favorite restaurant: " + data["favorite_restaurant"] + "<br />Gender: " + data["gender"]
);
}
});
return false;
});
});
----PHP-----
header('Access-Control-Allow-Origin: *');
header('Access-Control-Request-Method: POST');
header('Access-Control-Request-Headers: X-PINGOTHER');
if (is_ajax()) {
if (isset($_POST["action"]) && !empty($_POST["action"])) {
$action = $_POST["action"];
switch($action) {
case "test": test_function(); break;
}
}
}
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
function test_function(){
$return = $_POST;
$return["json"] = json_encode($return);
echo json_encode($return);