K. Brafford
K. Brafford

Reputation: 3839

Django problem with POST method

I am having an issue with using POSTed data instead of GET data with Django.

Here's my simplified urlpatterns class:

urlpatterns = patterns('',
    ('^hello/$', hello),
    ('^hello/ajax_info/$', ajax_info),   
)

And I have this view in the views.py file:

def ajax_info(request):
    if request.method == "POST":
        print "This is a post"
    # do stuff    

The web page served up when you browse /hello/ has this javascript in it:

    function loadXMLDoc(name) {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                //window.onload()

            }
        }

        // GET method
        //parm = values
        //xmlhttp.open("GET",name+"?q="+parm, true);
        //xmlhttp.send();

        // POST method
        parms = "data=" + values
        xmlhttp.open("POST", "ajax_info", false);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        xmlhttp.send(parms)
    } // end function loadXMLDoc

(in the web page I have a button that causes loadXMLDoc() to get called when you press it)

If I uncomment the "GET method" in the javascript, and comment out the "POST method" then my, django view gets called.

But if I comment out the "GET method" and use the "POST method" code, then my viewer doesn't even get called. And the django dev server returns this:

[28/May/2011 00:15:06] "POST /hello/ajax_info HTTP/1.1" 500 69236

Am I missing something obvious? The examples seem to indicate that my request should get to my viewer even if they are POSTs.

Upvotes: 0

Views: 4961

Answers (4)

solartic
solartic

Reputation: 4319

I remember at some point I had an issue where POST and ajax would not work properly when I forced the url to end with "/". I guess it's worth a shot to try and remove it.

urlpatterns = patterns('',
    ('^hello/$', hello),
    ('^hello/ajax_info$', ajax_info), # Removed ending slash   
)

Upvotes: 1

Stack Exchange User
Stack Exchange User

Reputation: 778

Your problem is that you do not understand how to debug ajax in browser.

Read this http://wiki.pylonshq.com/display/pylonscookbook/Debugging+AJAX+requests+using+Firebug and then use this technique to find what exactly crashes your ajax view.

Upvotes: 2

StefanNch
StefanNch

Reputation: 2609

I believe the problem is that your view method does not return anything ... Try accesing the page /hello/ajax_info/ and check if everything is working properly!

If this is the case, you will receive, with DEBUG = True, this error: "The view ... didn't return an HttpResponse object."

Also be sure you are using the "csrf_token" tag if you are POSTing something ... Cookies must be on!

Upvotes: 2

Eevee
Eevee

Reputation: 48536

I'm not familiar with django, but that 500 sure looks like an HTTP status code, indicating that your response was bad. Perhaps watch what happens with a JavaScript debugger: Firebug, Chrome's tools, Opera Dragonfly, etc.

Incidentally, you may want to use a library like jQuery for the client code. Manual ajax is a massive headache.

Upvotes: 2

Related Questions