在金字塔中: class ProjectorViews(Layouts):def __init__(self, request): self.request = request@view_config(renderer="json", name="updates.json", request_method="POST")def updates_view(self): print self.request.params JS: $(function(
class ProjectorViews(Layouts):
def __init__(self, request):
self.request = request
@view_config(renderer="json", name="updates.json", request_method="POST")
def updates_view(self):
print self.request.params
JS:
$(function() {
function get_updates () {
data = JSON.stringify({'a':1});
$.post('/updates.json', data, function(res) {
});
}, 'json').done(function() {
});
}
get_updates();
});
控制台显示self.request.params返回NestedMultiDict([(‘{“a”:1}’,u”)])
如何获取NestedMultiDict对象中的键和值?
如果我做self.request.params.getall(“a”),它会报告
KeyError: "Key not found: 'a'"
如果我做self.request.json_body,它报告
ValueError: No JSON object could be decoded$.post()始终使用application / x-www-form-urlencoded内容类型发送数据.使用$.ajax()发送具有正确内容类型的数据:
$.ajax({
url: url,
type: "POST",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(...);
在金字塔方面,request.json_body是正确的访问方式……好吧,请求的JSON主体.
