Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Dajaxice回调函数调用两次_Python_Ajax_Django_Dajaxice_Neo4django - Fatal编程技术网

Python Dajaxice回调函数调用两次

Python Dajaxice回调函数调用两次,python,ajax,django,dajaxice,neo4django,Python,Ajax,Django,Dajaxice,Neo4django,我使用的是django-dajaxice-ng包(django-dajaxicefork用于django1.5及以上版本,因为我使用的是django 1.5.4),我有一个奇怪的问题。 有时回调函数被调用两次甚至更多。 简单用法: ... {% load dajaxice_templatetags %} ... {% dajaxice_js_import %} <script> function update_chat(data) { messages =

我使用的是
django-dajaxice-ng
包(
django-dajaxice
fork用于
django
1.5及以上版本,因为我使用的是
django 1.5.4
),我有一个奇怪的问题。
有时回调函数被调用两次甚至更多。
简单用法:

...
{% load dajaxice_templatetags %}
...
{% dajaxice_js_import %}

<script>

    function update_chat(data) {
        messages = eval(data.messages);
        howmany=messages.length;
        if (howmany>0)
            console.log('called');
        for (i=0;i<howmany;i++) {
            M=messages[i];
            console.log(M.msg,M.sent);
        };
    }

    setInterval(function() {
            Dajaxice.myapp.receiveChat(update_chat,{'userID': {{otheruser.id}} });
    },2000);

 </script>
以下是控制台日志:

called
"Message" "16:09:28"
called
"Message" "16:09:28" 
如您所见,有时(实际上经常)会触发2到3次
update\u chat

我100%确信:

  • ajax函数receiveChat只调用一次
  • 显示两次的消息存储一次
我将
serialized_messages=json.dumps(messages)
,定义为
messages
一个dict列表。
结果如下:

'[{"msg": "Message", "sent": "16:09:28"}]'
在本例中,只有一条消息,但也适用于多条消息

我不得不进行双重序列化(另一个在
return
中),因为显然dajaxice只接受dict作为允许的返回值

有人也有这种行为吗?有线索吗

编辑 我使用的是
neo4django
,它使用的是
neo4j rest客户端
,我必须在一个模型上执行.save(),注意到只有通过这个保存我才遇到问题,可能是因为另一个HTTP请求

编辑2
这不是dajaxice的问题,切换到ajax
$。get()
会给出相同的结果,但我真的搞不懂。似乎并非所有浏览器都在这样做,当然Chrome也在这样做。

您是否检查过您的
sendChat
功能是否被多次调用?是的,没有;)该函数是存储消息的函数,正如我所写的,它只存储一次,因为该函数只调用一次。100%肯定是回调。也许发布很多js会有助于解决这个问题。我从未在django-dajaxice中经历过这种行为。实际上,我编辑了这个问题,因为相关部分不是发送,而是接收消息。添加了javascript代码(为了调试,我只是在console.log中显示聊天消息)。
'[{"msg": "Message", "sent": "16:09:28"}]'