【发布时间】:2014-07-16 16:11:02
【问题描述】:
我查看了很多答案,显示了如何在 python 方法中访问 json,但是我似乎无法让我的工作。
这是我的 ajax 调用
var data = {
'customer': customer,
'custID': custID,
'date': date,
'jobNum': jobNum,
'deviceID': deviceID
}
//create customer
if (custID === undefined) {
$.ajax({
url: "http://127.0.0.1:6543/test",
type: "POST",
data: JSON.stringify(data),
dataType: 'json',
success: function(response, textStatus, jqXHR) {
alert(response);
},
error: function(jqXHR, textStatus, errorThrown){
alert(textStatus, errorThrown);
}
});
}
else {
//empty
}
这是我的python方法:
@view_config(route_name="test", renderer='templates/main.html')
def new_sheet(request):
response = request.POST
myObject = json.loads(response)
#print myObject["customer"]
test = "hello"
return dict(test=test)
对 python 有点陌生,所以请原谅我有限的理解。如何获取我的 json 并访问对象属性?当我尝试打印时,我在 cmd 中得到的只是ValueError: No JSON object could be decoded
【问题讨论】:
-
为什么不使用
data: data,然后将每个元素作为单独的POST元素访问? -
@Barmar:这有其局限性。发送 JSON 是一个非常好的目标,尤其是当您想要发送结构化数据时。