【发布时间】:2018-01-30 08:32:43
【问题描述】:
我试图获取localStorage 的所有键/值并让它更新我的字典keywordDict,如下所示:
$(document).ready(function() {
var store = allStorage();
console.log(store);
$.ajax({
url: '/myUrl/Template/hint',
async: false,
type: 'POST',
data: {
'store': store
}
}).then(function(data) {
console.log(data.keywordDict);
});
})
allStorage()获取localStorage的所有键/值
function allStorage() {
var archive = {},
keys = Object.keys(localStorage),
i = keys.length;
while (i--) {
archive[keys[i]] = localStorage.getItem(keys[i]);
}
return archive;
}
从控制台我的 localStorage 返回键/值如下:
{hello{{hello}}: "hello{{hello}}", hello{{hello}: "hello{{hello}", hello{{hello: "hello{{hello", hello{{hell: "hello{{hell", hello{{hel: "hello{{hel", …}
h : "h"
he:"he"
hel:"hel"
hell:"hell"
hello:"hello"
hello{:"hello{"
hello{{:"hello{{"
hello{{h:"hello{{h"
hello{{he:"hello{{he"
hello{{hel:"hello{{hel"
hello{{hell:"hello{{hell"
hello{{hello:"hello{{hello"
hello{{hello}:"hello{{hello}"
hello{{hello}}:"hello{{hello}}"
在我的路线/myUrl/Template/hint 中,我更新了我的keywordDict 如下:
@app.route("/myUrl/Template/hint", methods=['GET','POST'])
def gethint():
keywordDict = {}
keyword = request.form.get('store')
print "keyword is ",keyword
if keyword:
keywordDict.update({keyword:keyword})
print keywordDict
return jsonify(keywordDict=keywordDict)
但是,我的keywordDict 永远不会更新,因为keyword 总是返回none。
然后,如果我去打印request.form,它的返回值如下:
(
[
('store[hello{{hel]', u 'hello{{hel'),
('store[hello{{hello}]', u 'hello{{hello}'),
('store[hello{]', u 'hello{'),
('store[h]', u 'h'),
('store[hel]', u 'hel'),
('store[hello{{h]', u 'hello{{h'),
('store[hello]', u 'hello'),
('store[he]', u 'he'),
('store[hell]', u 'hell'),
('store[cloneobjIndex]', u ''),
('store[hello{{hello}}]', u 'hello{{hello}}'),
('store[hello{{hello]', u 'hello{{hello'),
('store[hello{{hell]', u 'hello{{hell'),
('store[hello{{he]', u 'hello{{he'),
('store[hello{{]', u 'hello{{')
]
)
但是,我的keywordDict 仍然没有更新。
我的keywordDict 出了什么问题,如何根据我的localStorage 更新键/值?谢谢。
【问题讨论】:
-
似乎问题是 .get('store'),也许你应该检查 python 语法。我不懂python,,,所以,帮不了你
-
@xianshenglu,谢谢。我很确定
request.form.get是为了从表单中获取价值。 -
我猜问题出在 API .get() 中的“存储”
-
@xianshenglu,我猜是的,所以我只打印
request.form,它返回结果如我上面的问题,它的数据类型是ImmutableMultiDict. -
当您从 ajax 传递对象并在 python 中反序列化时,可以尝试将对象序列化为 json 字符串。
标签: javascript python dictionary