【发布时间】:2013-07-24 01:17:14
【问题描述】:
我通过 jQuery Ajax 以 JSON 的形式将大量数据推送到 Django 应用程序。数据被服务器接收到了,但是我解析不了。
在我的视图文件中,我有
from django.http import HttpResponse
from django.utils import simplejson
def saveStrategy(request):
if not request.user.is_authenticated:
return HttpResponse(simplejson.dumps({"response" : "failure"}))
else:
#This section here throws a TypeError - Expected String or Buffer
#update_tasks = simplejson.loads(request.POST.get("tasks", False))
#update_strategy = simplejson.loads(request.POST.get("strategy", False))
update_strategy = request.POST.get("strategy", False)
update_tasks = request.POST.get("tasks", False)
print update_strategy
return HttpResponse(simplejson.dumps({"response" : "success"}))
这是
的目的地if (typeof currentTaskId !== "undefined") {
$("#save_strategy_task").trigger("click");
localTasks = JSON.stringify(tasks)
}
else
localTasks = {};
.ajax({
url : "saveStrategy/",
type : "POST",
data : {"strategy" : JSON.stringify(strategy), "tasks" : localTasks},
dataType : "json",
success : function(data) {
if (data.response == "success")
strategyDetailCloseHandler();
}
});
现在,当update_strategy 不参与simplejson 时,我得到
{
"title":"Title",
"status":"pending",
"strategy":"Strategy",
"dueDate":"",
"owner":"",
"metrics":"test",
"id":"3",
"outcome":"Outcome1"
}
但是,如果我尝试
print update_strategy["id"]
我收到TypeError: string indices must be integers 错误。
如何解析传入的 JSON 以更新现有的模型对象?
【问题讨论】:
-
strategy_update是什么? -
一个错字,现已修正,并与我现有的代码仔细检查