Reputation: 67
I want to send json data via ajax fr4om template to view and put it directly in database.
the problem is that the data does not reach the view.
(when i insert numbers it works but when i try to insert my json data: json_data['x']
,json_data['y']
,json_data['z']
it doesn't works)
any ideas ??
url.py
url(r'^ajaxexample_json$', 'myApp.views.ajax'),
Template:
$.ajax({
type: "POST",
contentType: "application/json",
url : "http://localhost:8000/ajaxexample_json",
data : { x: "1", y: "2" , z: "3" },
dataType: "json"
});
view.py
def ajax(request):
db = db_connection().db;
db_manager= db_management(db);
json_data=json.loads(request.body)
db_manager.insert_access_point(json_data['x'],json_data['y'],json_data['z'])
db.close()
return HttpResponse('Ok')
Upvotes: 0
Views: 1321
Reputation: 7328
jQuery will not take an object and turn it into JSON. You must do that yourself using a function like JSON.stringify
data : JSON.stringify({ x: "1", y: "2" , z: "3" }),
Upvotes: 1