小编Phi*_*hil的帖子

在javascript中通过python-dictionary-turned-into-json循环

在编写一个django应用程序时,我在jQuery ajax调用上返回以下json:

{
    "is_owner": "T", 
    "author": "me",
    "overall": "the surfing lifestyle", 
    "score": "1", 
    "meanings": {
        "0": "something", 
        "1": "something else", 
        "3": "yet something else", 
        "23": "something random"
        }, 
    "user vote": "1"
}
Run Code Online (Sandbox Code Playgroud)

在javascript/jQuery回调函数中,我可以轻松地访问is_owner,author等.

is_owner = json.is_owner;
author = json.author;
Run Code Online (Sandbox Code Playgroud)

但是对于意义,数字是不同的,取决于它从服务器拉出的内容.在服务器端的意义部分,现在我正在做的是构建一个像这样的字典:

meanings_dict = {}
meanings = requested_tayke.meanings.all()
for meaning in meanings:
    meanings_dict[meaning.location] = meaning.text
Run Code Online (Sandbox Code Playgroud)

然后返回一个我创建的json:

test_json = simplejson.dumps({'is_owner':is_owner, 'overall':overall, 'score':str(score),'user vote':str(user_vote), 'author': author, 'meanings' : meanings_dict })
print test_json

return HttpResponse(test_json)
Run Code Online (Sandbox Code Playgroud)

我的问题是: 如何在javascript中从我的json访问"含义"数据? 我需要遍历所有这些.也许我需要以不同的方式将它加载到json中.我完全控制服务器和客户端,所以我愿意改变它以使其工作. 另外值得注意的是:我没有使用Django的序列化功能.我不能让它适应我的情况.

javascript django jquery json

4
推荐指数
1
解决办法
5229
查看次数

我如何使 Sinon.JS callCount 递增

所以我有一个这样的 Chai/Mocha/Sinon 测试:

import sinon from 'sinon'

describe(`My Test`, () => {
  it(`should track the number of calls`, () => {
    function testMe() {
      console.log(`test me`)
    }
    const spy = sinon.spy(testMe)
    testMe()
    console.log(spy.getCalls())
    console.log(spy.callCount)
  })
})
Run Code Online (Sandbox Code Playgroud)

测试运行时,会记录以下内容:

test me
[]
0
Run Code Online (Sandbox Code Playgroud)

这令人费解。我究竟做错了什么?

javascript mocha.js sinon chai

2
推荐指数
1
解决办法
676
查看次数

如何将我已经定义的javascript函数传递给jquery调用?

例如,如果我有:

function example() {
    alert("example");
}
Run Code Online (Sandbox Code Playgroud)

那么为什么这不起作用?

$("h2").click( example() );
Run Code Online (Sandbox Code Playgroud)

是否必须内联定义?

javascript jquery

1
推荐指数
1
解决办法
253
查看次数

标签 统计

javascript ×3

jquery ×2

chai ×1

django ×1

json ×1

mocha.js ×1

sinon ×1