I'm using Django for my webapp. I'm sending a JSON data to my views but I'm not able to access Nodes and edges by calling decoded_data['nodes'] and it gives me :
'NoneType' object is not subscriptable
error message.
Here is how I send json to to my view:
var a={
nodes: thisGraph.nodes,
edges: saveEdges
};
//send data to server
$(document).ready(function(){
function change(){
$.ajax({
type:"POST",
url: "/",
data: {'data': JSON.stringify(a)},
dataType: "json",
success: function(){
console.log("Ajax worked");
$('#message').text("Ajax worked");
},
headers:{'X-CSRFToken': csrftoken}
});
here is my view:
data = request.POST.get("data")
json_encoded = json.dumps(data)
decoded_data = json.loads(json_encoded)
logger.error(decoded_data['nodes'])
the decoded_data looks like this:
{"nodes":[{"type":"node","title":"new concept","id":0,"x":658,"y":100},{"type":"
constraint","id":2,"title":"new Constraint","x":371,"y":95}],"edges":[{"source":
2,"target":0}]}
I appreciate your help
data={"nodes":[{"type":"node","title":"new concept","id":0,"x":658,"y":100},{"type":" constraint","id":2,"title":"new Constraint","x":371,"y":95}],"edges":[{"source": 2,"target":0}]}and there are no errorsprint data?myDict = dict(data.iterlists())and see what happens (It seems like you have a QueryDict stackoverflow.com/questions/13349573/… )decoded_data = json.loads(json_encoded[0])I did that it saysExpecting value: line 1 column 1 (char 0)