0

site.com/api/index.php is where I need the ajax request to go. From site.com/sub/ the request works perfectly but sub.site.com is sending the request to sub.site.com/api/index.php which obviously does not exist... I've Google and StackOverflowed the hell out of the question, but can't seem to find an answer that works.

Code:

var jQuery_ajax = {
   url: "site.com/api/index.php",
   type: "POST",
   data: $.param(urlData),
   dataType: "json"
}


var request = $.ajax(jQuery_ajax);

The most common answer was to set document.domain to the regular site, but that does not seem to do anything... I've also seen answers talking about iFrames, but I want to stay away from iFrames at all costs.

document.domain = "site.com";

** Note: everything is on the same server.

HACKY SOLUTION: made sub.site.com/api/index.php a file that simply reads

include_once("$path2site/api/index.php");
3
  • jsonp would be the easiest solution, followed by CORS and then followed by server-side proxy. don't forget the http:// part. Commented Sep 24, 2013 at 19:58
  • I think the call is hitting the wrong url because you have site.com/api/index.php instead of http://site.com/api/index.php. Once you fix that, you'll have to deal with the cross domain issues. Commented Sep 24, 2013 at 20:01
  • I've tried with the Http:// and you are correct -- I get an "is not allowed by Access-Control-Allow-Origin." error Commented Sep 24, 2013 at 20:02

1 Answer 1

1

Once you've corrected the URL to http://site.com/api/index.php try adding the following to api/index.php:

header("Access-Control-Allow-Origin: http://sub.site.com");

e: it's possible that doing so may disallow use from site.com as well; I'm not seeing a way to provide two values, so you may need a way to tell it which site to use, like a ?sub=1 arg to index.php

Sign up to request clarification or add additional context in comments.

5 Comments

Just tried it. Doesn't work.. still get "POST sub.site.com/api/index.php 404 (Not Found)" in the console log.
Are you sure you corrected the url arg? Can you edit your question to match your current code?
One problem... now accessing site.com/api/index.php from site.com (without sub) does not work...
this works. Thanks again Collin if (isset($_SERVER['HTTP_ORIGIN'])){ $allow = array(); $allow[] = "http://wwww.site.com"; $allow[] = "http://site.com"; $allow[] = "http://sub.site.com"; if (in_array($_SERVER['HTTP_ORIGIN'],$allow)){ header("Access-Control-Allow-Origin: ".$_SERVER['HTTP_ORIGIN']); } }
To answer on why it works after fixing the url arg: "site.com/api/index.php" is not a well-formed URL; it makes sense to us humans, but software/frameworks/browsers/etc expect a well-formed one which includes the http:// :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.