gpt4 book ai didi

javascript - 从本地存储更新字典?

转载 作者:行者123 更新时间:2023-12-03 02:36:24 24 4
gpt4 key购买 nike

我尝试获取 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更新键/值?谢谢。

最佳答案

当您从ajax传递对象并在python中反序列化时,您可以尝试将对象序列化为json字符串。

关于javascript - 从本地存储更新字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48517047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com