我尝试获取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
更新密钥/值?感谢。
答案 0 :(得分:1)
当您从ajax传递对象并在python中反序列化时,可以尝试将对象序列化为json字符串。