Code

Opened 6 years ago

Closed 6 years ago

#6613 closed (invalid)

XmlHttpRequests to simple views fail to return in MSIE

Reported by: kevin@… Owned by: nobody
Component: HTTP handling Version: master
Severity: Keywords: MSIE, XMLHttpRequest, views, Json
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I've been using Django for some UI prototyping work to return templates as well as data responses to XMLHttpRequests (XML, XHTML, JSON, etc.) and came across a really unusual bug.

In short, if I write a really simple view like this:

def get_json_data(request):
    """Returns some dummy JSON-encoded data"""
    jsondata = build_json_data()
    return http.HttpResponse(jsondata, mimetype='text/javascript')

Then call that view via XMLHttpRequest from any version of MSIE, MSIE will report that the request has failed when clearly Django returned the data just fine (reports error code 200, and identical code works fine in Firefox).

However I found -- quite by accident, as it turns out -- that one of my views always worked in MSIE. Through process of elimination I realized the only difference: I was printing the content of request.POST to the console for debugging purposes:

def get_json_data(request):
    """Returns some dummy JSON-encoded data"""
    print request.POST
    jsondata = build_json_data()
    return http.HttpResponse(jsondata, mimetype='text/javascript')

With that one extra line, MSIE accepts the response every time. This doesn't seem like a good fix for production environments -- and maybe it doesn't matter, as I haven't verified that the problem exists there -- but it certainly caused me a lot of frustrating trying to validate my code under MSIE, and wanted to save people some grief in case they have encountered similar problems.

Attachments (0)

Change History (3)

comment:1 Changed 6 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

For us to have any chance of working if there's actually a Django bug here, we'll need some more information.

  1. What is your server setup (e.g. Apache + mod_python, or something fastcgi based, or something else)?
  2. Are you sending anything at all to the view function (any POST or GET parameters)? If so, does the problem persist if you don't send any such parameters?

It's just possible that because you're not accessing anything in the 'request' object, there's some data in a pipe waiting to be read and IE isn't going to read a reply until it's pushed out the full request. This would only be the case, though, if you were sending through some request data, hence the second question above.

I suspect there's no bug here (at least, not in Django), but if you could provide this information, we might be able to dig a little deeper.

comment:2 Changed 6 years ago by kevin@…

  1. I am using the Django built-in development server environment (e.g. /usr/bin/python manage.py runserver); no other web server used/tested for this issue.
  2. Yes, I am sending data -- and it appears as though this is the problem, because if I change the XMLHttpRequest to not post any data, the response also returns fine.

I am a little foggy on the HTTP protocol and its implementation in Django; I had assumed that the response object passed to the view contained the entire response as opposed to a handle whose attributes must be explicitly expected by the view in order to complete the expected transaction. Thank you for the clarification.

I apologize for my misunderstanding. Please close the ticket

comment:3 Changed 6 years ago by mtredinnick

  • Resolution set to invalid
  • Status changed from new to closed

Your second paragraph isn't generally true, however it's clearly playing a role here that when you're sending data you need to consume it. That's quite possibly just a side-effect of the way the development server is implemented. It's meant to be fairly simple, after all.

You'll probably want to raise further questions about this on the django-users list when you have them. Note that doing almost all non-trivial AJAX work is going to lead to some difficulties with the development server, since it's only single threaded. So anything needing multiple simultaneous requests won't work.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.