从localStorage更新字典?

时间:2018-01-30 08:32:43

标签: javascript python dictionary

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

1 个答案:

答案 0 :(得分:1)

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