0

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);
dda
  • 6,030
  • 2
  • 25
  • 34
chaky
  • 117
  • 2
  • 13
  • http://en.wikipedia.org/wiki/Same_origin_policy Is the hostname/port/scheme for the ajax call url the same as the web page that triggers it? – Joachim Isaksson May 12 '13 at 14:28
  • Why do you think _my problem is different_? It seems the same as all the other questions about cross-origin AJAX. – Barmar May 12 '13 at 14:35
  • Have a look at this question: http://stackoverflow.com/questions/3897641/can-ajax-request-data-from-a-remote-server. One of the links provided there also demonstrates how to make a PHP script to support JSONP. It is much easier to do than messing with cross-domain policies (and always works). – Lukas May 12 '13 at 14:39
  • @ Joachim Isaksson : they are on two different servers. – chaky May 12 '13 at 16:00
  • @ Barmar : Coz my code works when they are on the same domain – chaky May 12 '13 at 16:01
  • @ Lukas : I tried the JSONP and didn't work. Both of the servers are amazon-ec2 so I even tried with host-gator(with no root) server with amazon (with root) to check if it's the server setup. – chaky May 12 '13 at 16:03
  • If they're on different servers, it's a "cross origin" call and I would think many matching answers how to make that work (JSONP being one option) are already available on this site. – Joachim Isaksson May 12 '13 at 17:36
  • If you enter the php URL in the browser does it find something? http://********/response.php If you are presented with a 404 then it's just an incorrect URL. You shouldn't need to play around with access policies in this situation – Jackson May 12 '13 at 14:27
  • Thanks Jackson...the URL is correct. and I do not get any errors when call the PHP url alone – chaky May 12 '13 at 15:57

0 Answers0