11

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.

Baversjo
  • 3,656
  • 3
  • 34
  • 47

5 Answers5

4

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
  • 415
  • 3
  • 9
0

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

Community
  • 1
  • 1
SAGExSDX
  • 683
  • 6
  • 12
  • I guess you could do an IFRAME workaround http://www.tomhoppe.com/index.php/2008/03/cross-sub-domain-javascript-ajax-iframe-etc/ – SAGExSDX Jun 28 '10 at 14:27
  • 1
    According to the same origin policy, it is a different domain: http://en.wikipedia.org/wiki/Same_origin_policy – wsanville Jun 28 '10 at 14:27
0

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
  • 5,296
  • 4
  • 39
  • 50
0

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
  • 8,961
  • 12
  • 49
  • 77
-1

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
  • 8,795
  • 3
  • 31
  • 45