开发者

Ajax request not receiving xml from Django

开发者 https://www.devze.com 2022-12-16 10:15 出处:网络
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

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 'galleryimage开发者_开发百科s.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?


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


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/ )

0

精彩评论

暂无评论...
验证码 换一张
取 消