Send httpresponse to client

Hi all,
new here :slight_smile:

i have this python function

def gtest(request):
    p = request.POST
    print p
    g = p['information']
    print g
    to_json = {
        "key1": "value1",
        "key2": "value2"
    }
   jsonValidateReturn = simplejson.dumps({"jsonValidateReturn": "ddddd"})
    return HttpResponse(jsonValidateReturn, content_type='application/json' ,mimetype='application/json') 

and on the client side

$.ajax({
        url: "http://127.0.0.1:8000/api/gtest/",
        type: "POST",
        data: { information : "You have a very nice website, sir."},
        dataType: "json",
        success: function(data) {
        	alert ("post is success");
        },
        error: function(request,error) 
        { 
        alert(request.responseText);
        alert(error);
        }
    });

no matter what i do when the post is back it goes to error.
on my server i see this message
“POST /api/gtest/ HTTP/1.1” 200 31
so it looks like response should work but no matter what it does not go to the success function…

plz help :\

thx
Guy

Could you send a message to a moderator (anyone whose name is green or blue) and ask to have your code edited? Or post again, but wrap your code in code tags ([noparse]

 to start, close it with 

[/noparse]). Otherwise the forums eat up your indentation which with Python we need. :slight_smile:

Also: Python 2x or 3x? Are you using httplib?


return HttpResponse(jsonValidateReturn, content_type='application/json' ,mimetype='application/json') 

What are the required arguments for this (I see a different list on the library pages at docs.python.org for version 2)? I ask because MIMEtype should be represented by Content Type as far as the browser is concerned.

When you run your Javascript, in your browser debugger, can you see anything else being returned? What errors are being checked in the
success: function(data) {
alert (“post is success”);
},
error: function(request,error)
{
alert(request.responseText);
alert(error);
}

section? Those success and errors should refer to the ajax readyStates I think.
You may not be getting a readyState 4. Here are the states, and you should be able to see which one you get in your browser debugger as you step through your Javascript.

Also back when I was doing ajax by hand I needed to add in a send() call just to make Opera happy, and I dunno if Opera fixed this or if jQuery (is that what you’re using) already fixes this.