views:

57

answers:

5

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.

A: 

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

SAGExSDX
but it's not cross domain!
Baversjo
I guess you could do an IFRAME workaroundhttp://www.tomhoppe.com/index.php/2008/03/cross-sub-domain-javascript-ajax-iframe-etc/
SAGExSDX
According to the same origin policy, it is a different domain: http://en.wikipedia.org/wiki/Same_origin_policy
wsanville
Okey. That was exactly what I was looking for I guess. Thanks
Baversjo
+1  A: 

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.

Bogdan Constantinescu
A: 

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.

Russell Leggett
If this works, I would think this would be the best answer.
SAGExSDX
It's already set to the top level domain as described above
Baversjo
A: 

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.

eos87
+1  A: 

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.

Rob