Reputation: 6859
How can I get the data passed from the ajax in Django?
In the js, I use ajax to pass the data:
$.ajax({
type:'post',
url:'/app_admin/myServerDetail/',
data:JSON.stringify({'server_id':server_id}), // I want to pass the server_id to views.py
dataType:'json',
success:success_func
})
In my views.py, I print the request.POST, and server_id:
server_id = request.POST.get("server_id")
print (server_id, request.POST)
The result is bellow:
(None, <QueryDict: {u'{"server_id":"f55dbe56-7fa8-4d13-8e1e-2dc67499b6ac"}': [u'']}>)
So, how can I get the server_id in my views.py ?
Upvotes: 0
Views: 51
Reputation: 26924
If you want to get the json stringifyed data passed by ajax like your case, you should use json to loads the request.body.
server_id = json.loads(request.body).get("server_id")
Upvotes: 1
Reputation: 1983
try changing this `data:JSON.stringify({'server_id':server_id})`
to this data:{'server_id':server_id}
Upvotes: 0