Jquery .load and subdomains
I am trying to load a file (http://domain.com/v2/inc/review.php) from a subdomain (http://resort.domain.com) using jquery. Although I use the full location it refuses to load. Does anyone know开发者_如何学C how to get it to work?
$("#resort").load("http://domain.com/v2/inc/review.php");
To make things short. You can't load remote domain content with ajax. This a restriction applied by all browsers and is called the Same origin policy. So trying to load data from a different subdomain is already a violation of the same origin policy.
The only thing you can do to load data from a different domain is use json
jQuery.getJSON()
@Matt's comment:
I was indeed thinking about JSONP. Which is exactly what jQuery getJson uses if the url is on a remote server
From the documentation:
If the specified URL is on a remote server, the request is treated as
JSONP
instead. See the discussion of the jsonp data type in $.ajax() for more details.
In the Javascript for the page on http://resort.domain.com, do...
document.domain = "domain.com";
Then you should be fine.
You are running in to the security restriction that browsers have with doing an ajax request from a different domain. One option is to proxy your request through your current domain. Something like Simple PHP Proxy would work for you.
Using document.domain is the most compatible method with the most flexibility.
document.domain = "domain.com";
Here is a good summary presentation of different cross-domain techniques: http://www.slideshare.net/SlexAxton/breaking-the-cross-domain-barrier
But it is of utter importance that no one else can setup subdomains on that domain, because if they can, they can use it for fishing data.
精彩评论