Ajax calls to subdomain

I have one server located at example.com running apache, serving my static html files.

I also have a json service located at api.example.com running python with cherrypy.

The user requests example.com and get the index html page. On that page I make an ajax request with jquery to the json service. document.domain returns example.com

        $.ajax({
        type: 'GET',
        url: 'http://api.example.com/resource/',
        dataType: 'json',
        success: successCallback,
        error: errorHandler
    });

However, I can't see the response body for the ajax request in firebug. This leads me to believe that the browser (FF) doesn't support this.

What are the best methods to achieve this? I would prefer not to use any proxying on the apache backend for example.com if possible.

Answers


You can also use JSONP by adding callback=? to the end of the url. jQuery already knows how to handle these type of requests but it does require some server side changes to handle the callback param.


AJAX request is only supported on the same domain. However, you can write an http proxy in your preferred scripting language and make calls to that http proxy. You can check out this little tutorial on an AJAX proxy written in php.


try changing your domain in your sub-domain, like this

<script type="text/javascript">    
  document.domain = 'example.com';
</script>

if does not work, change your document.domain in your domain page too.


As far as I know, you can't do AJAX cross-domain.

Why is cross-domain Ajax a security concern?

Though I guess you could do an IFRAME workaround

Cross Sub Domain Javascript


Use document.domain to make the domain be the top level domain instead of the subdomain.

document.domain="example.com"

This is described in detail on MDN.


Need Your Help

site.master tells pages to wait loading...but site kind of still comes up?

javascript jquery html asp.net

Aside from it not being "recommended" we needed to do this for a specific project. Basically I've got a site master page that is displays a "Please wait while loading..." div to not display a page...

How does RSpec's expect... to change... to... work internally?

ruby-on-rails ruby rspec

In RSpec, there is matcher expect{}.to change{}.to like