My Django view:
class Main(View):
def post(self, request, *args, **kwargs):
obj = json.loads(request.raw_post_data)
//do something with obj
temp = Entry.objects.all();
result = []
for t in temp:
result.append( (t.key,t.value) );
return HttpResponse(json.dumps(result), content_type="application/json")
My javascript:
$.post("http://XXX/", JSON.stringify({}), function(data) {
alert("OK");
}, "json");
What firebug has to say:
POST http://193.0.96.129:8006/ 200 OK
Post Headers
Response headers
Content-Type application/json
Date Thu, 16 May 2013 00:00:28 GMT
Server WSGIServer/0.1 Python/2.7.3
Request headers
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language pl,en-us;q=0.7,en;q=0.3
Content-Length 2
Content-Type application/x-www-form-urlencoded; charset=UTF-8
Host XXX
Origin null
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0
The data gets through to Django, but the success callback doesn't get called. Django server returns code 200, but in Firebug notification about the POST is red.
I tried multiple variations of the $.post call, none worked. If I use $.ajax call and specify error callback
error: function(xhr, textStatus, errorThrown) {
alert("some error" + xhr.responseText+ " X " + textStatus + " X " + errorThrown);
},
all I get to know is that "error" happend (xhr.responseText == textStatus == "error").
It seems like I was trying to do a "Cross-Domain" call. I was testing running the .html file locally, trying to call my remote Django server. Changing path of POST destination to relative, and running the script through Django view fixed the problem.
Thank you Daniel, the Content-Length thing was my carelessness, I posted different view version then one that we see responding in Firebug (it was returning "{}" I think - I was trying to debug).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.