如何使用jQuery进行子域Ajax调用(不使用iFrame) [英] How to make a subdomain ajax call with jQuery (without iFrames)
问题描述
site.com/api/index.php 是我需要ajax请求去的地方.从 site.com/sub/可以正常运行请求,但是 sub.site.com 会将请求发送到 sub.site.com/api/index.php 显然不存在...我已经让Google和StackOverflow摆脱了困境,但似乎找不到有效的答案.
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.
代码:
var jQuery_ajax = {
url: "site.com/api/index.php",
type: "POST",
data: $.param(urlData),
dataType: "json"
}
var request = $.ajax(jQuery_ajax);
最常见的答案是将 document.domain 设置为常规站点,但这似乎无济于事...我也看到了有关iFrame的答案,但是我想不惜一切代价远离iFrame.
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解决方案:将sub.site.com/api/index.php设置为简单读取的文件
HACKY SOLUTION: made sub.site.com/api/index.php a file that simply reads
include_once("$path2site/api/index.php");
推荐答案
将URL纠正为http://site.com/api/index.php
后,请尝试将以下内容添加到api/index.php
:
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:这样做有可能也会禁止site.com的使用;我没有找到提供两个值的方法,因此您可能需要一种方法来告诉它使用哪个站点,例如index.php的?sub = 1 arg
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
这篇关于如何使用jQuery进行子域Ajax调用(不使用iFrame)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!