【发布时间】:2014-05-20 01:02:23
【问题描述】:
我遇到了一个似乎无法解决的问题。任何见解都会很棒。
该脚本应该从数据库中获取内存分配信息,并将该信息作为格式化的 JSON 对象返回。当我给它一个静态 JSON 对象时,该脚本可以正常工作,它将 stack_ids(我将传递的信息),但是当我尝试通过 POST 传递信息时它不会工作。
虽然我的代码的当前状态使用request.json("") 来访问传递的数据,但我也尝试过request.POST.get("")。
我的 HTML 包含这个帖子请求,使用 D3 的 xhr 帖子:
var stacks = [230323, 201100, 201108, 229390, 201106, 201114];
var stack_ids = {'stack_ids': stacks};
var my_request = d3.xhr('/pie_graph');
my_request.header("Content-Type", "application/json")
my_request.post(stack_ids, function(stuff){
stuff = JSON.parse(stuff);
var data1 = stuff['allocations'];
var data2 = stuff['allocated bytes'];
var data3 = stuff['frees'];
var data4 = stuff['freed bytes'];
...
...
}, "json");
虽然我的服务器脚本有这条路线:
@views.webapp.route('/pie_graph', method='POST')
def server_pie_graph_json():
db = views.db
config = views.config
ret = {
'allocations' : [],
'allocated bytes' : [],
'frees' : [],
'freed bytes' : [],
'leaks' : [],
'leaked bytes' : []
}
stack_ids = request.json['stack_ids']
#for each unique stack trace
for pos, stack_id in stack_ids:
stack = db.stacks[stack_id]
nallocs = format(stack.nallocs(db, config))
nalloc_bytes = format(stack.nalloc_bytes(db, config))
nfrees = format(stack.nfrees(db, config))
nfree_bytes = format(stack.nfree_bytes(db, config))
nleaks = format(stack.nallocs(db, config) - stack.nfrees(db, config))
nleaked_bytes = format(stack.nalloc_bytes(db, config) - stack.nfree_bytes(db, config))
# create a dictionary representing the stack
ret['allocations'].append({'label' : stack_id, 'value' : nallocs})
ret['allocated bytes'].append({'label' : stack_id, 'value' : nalloc_bytes})
ret['frees'].append({'label' : stack_id, 'value' : nfrees})
ret['freed bytes'].append({'label' : stack_id, 'value' : nfree_bytes})
ret['leaks'].append({'label' : stack_id, 'value' : nleaks})
ret['leaked bytes'].append({'label' : stack_id, 'value' : nfree_bytes})
# return dictionary of allocation information
return ret
大部分都可以忽略,当我给它一个充满数据的静态 JSON 对象时,脚本就可以工作了。
请求当前返回 500 Internal Server Error: JSONDecodeError('Expecting value: line 1 column 2 (char 1)',)。
谁能向我解释我做错了什么?
另外,如果您需要我进一步解释或提供任何其他信息,我很乐意这样做。搞了这么久,脑子有些发麻,可能漏掉了什么。
【问题讨论】:
-
你可以在解码之前发布json吗?在我看来好像没有数据。
标签: python post d3.js xmlhttprequest bottle