0

I have a Django server which handles requests to a URL which will return some HTML for use in an image gallery. I can navigate to the URL and the browser will display the HTML that is returned, but I can't get that same HTML by doing an AJAX call (using jQuery) to the same URL.

This is the view that generates the response:

def gallery_images(request, gallery_name):
    return render_to_response('galleryimages.html', {'images': get_images_of_gallery(gallery_name)}, mimetype='text/xml')

This is the 'galleryimages.html' template:

{% for image in images %}
<div id="{{image.name}}big">
    <div class="actualImage" style="background-image:url({{image.image.name}});">
        <h1>{{image.caption|safe}}</h1>
    </div>
</div>
{% endfor %}

This is the jQuery call I am making:

$("#allImages").load("http://localhost:8000/galleryimages/Web");

However, this loads nothing into my #allImages div. I've used firebug and ran jQuery's Ajax method .get("http://localhost:8000/galleryimages/Web") and firebug says that the response text is completely empty.

When I check my Django server log, this is the entry I see for when I navigate to the URL manually, through my browser:

[16/Jan/2010 17:34:10] "GET /galleryimages/Web HTTP/1.1" 200 215

This is the entry in the server log for when I make the AJAX call:

[16/Jan/2010 17:36:19] "OPTIONS /galleryimages/Web HTTP/1.1" 200 215

Why does the AJAX request not get the xml that my Django page is serving?

ampersandre
  • 3,156
  • 3
  • 29
  • 37
  • Why specify you're sending XML (and then send an incorrect XML documents, one lacking a top-level element) when jQuery's `load` is designed to receive HTML and it does seem that it's HTML that you're sending? I doubt that's responsible for that weird HTTP `OPTIONS` request, and thus your problem, but it surely can't help and just complicates the issue to no good end (browsers are designed to be incredibly tolerant of bad data, so it's no good test that the data is visible in one of them;-). – Alex Martelli Jan 17 '10 at 00:05
  • Ah thanks! That's probably step 1 to getting this to work. I've changed the mimetype to text/html, but unfortunately, the ajax request still comes up as an OPTIONS method and it isn't receiving anything. – ampersandre Jan 17 '10 at 00:22
  • does it work if you take out the whole mimetype part? – Brandon Henry Mar 16 '10 at 13:29

2 Answers2

0

You want to specify mimetype='application/xml'.

Peter Rowell
  • 17,605
  • 2
  • 49
  • 65
0

The problem was that JQuery was noticing that the URL I was requesting was on another domain, and in an effort to stop cross-domain scripting, converted my GET request to an OPTION request.

The solution to this was to write a PHP page that would accept a URL as a query parameter, and send the AJAX request to this PHP proxy page. The proxy page would pull down the URL I passed in and send it back.

(see here: http://www.abdulqabiz.com/blog/archives/2007/05/31/php-proxy-script-for-cross-domain-requests/ )

ampersandre
  • 3,156
  • 3
  • 29
  • 37