Python/Battle:通过post发送JSON对象
我遇到了一个我似乎无法克服的问题。任何洞察都会很好 脚本应该从数据库获取内存分配信息,并将该信息作为格式化的JSON对象返回。当我给它一个静态JSON对象时,该脚本运行良好,它将堆叠我将要传递的信息,但当我试图通过POST传递信息时,它将不起作用 尽管代码的当前状态使用request.json访问传递的数据,但我也尝试了request.POST.get My HTML使用D3的xhr post包含此post请求:Python/Battle:通过post发送JSON对象,python,post,d3.js,xmlhttprequest,bottle,Python,Post,D3.js,Xmlhttprequest,Bottle,我遇到了一个我似乎无法克服的问题。任何洞察都会很好 脚本应该从数据库获取内存分配信息,并将该信息作为格式化的JSON对象返回。当我给它一个静态JSON对象时,该脚本运行良好,它将堆叠我将要传递的信息,但当我试图通过POST传递信息时,它将不起作用 尽管代码的当前状态使用request.json访问传递的数据,但我也尝试了request.POST.get My HTML使用D3的xhr post包含此post请求: var stacks = [230323, 201100, 201108, 229
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内部服务器错误:JSONDecodeError'Expecting value:line 1 column 2 char 1'
谁能解释一下我做错了什么
此外,如果您需要我进一步解释,或包括任何其他信息,我很乐意这样做。在做了这么长时间的工作后,我的大脑有点兴奋,所以我可能错过了一些东西。以下是我对POST的处理方法,它是有效的:
from bottle import *
@post('/')
def do_something():
comment = request.forms.get('comment')
sourcecode = request.forms.get('sourceCode')
有了的积分,您可以在解码之前发布json吗?在我看来,似乎没有数据。
function saveTheSourceCodeToServer(comment) {
var path = saveLocation();
var params = { 'sourceCode' : getTheSourceCode() , 'comment' : comment};
post_to_url(path, params, 'post');
}