Javascript Json响应错误

Javascript Json响应错误,javascript,python,tornado,uncaught-exception,Javascript,Python,Tornado,Uncaught Exception,我有个问题 我有一个向传感器网络发送命令的页面 当我点击这部分代码时 <a href='javascript:void(send_command_to_network("{{net.id}}", "restartnwk"));'>Restart Network <i class="icon-repeat"></i> </a> 此函数用于构建一个url,以调用用python编写的Tornado web服务器中的处理程序。处理程序如下所示: clas

我有个问题

我有一个向传感器网络发送命令的页面

当我点击这部分代码时

<a href='javascript:void(send_command_to_network("{{net.id}}", "restartnwk"));'>Restart Network <i class="icon-repeat"></i> </a>
此函数用于构建一个url,以调用用python编写的Tornado web服务器中的处理程序。处理程序如下所示:

class NetworkSendHandler(BaseHandler):
# Requires authentication 
@tornado.web.authenticated
def get(self, nid):
    # Get the command 
    command = self.get_argument('command').upper(); 

    # The dictionary to return
    ret = {}

    #Check if the command is available
    if command not in ['RESTARTNWK']:
        raise tornado.web.HTTPError(404, "Unknown command: " + str(command))


    #Command ZDP-RestartNwk.Request
    if command == 'RESTARTNWK':
        op_group = "A3"
        op_code = "E0"
        packet_meta = "*%s;%s;%s;#"
        pkt_len = hextransform(0, 2)

        packet = packet_meta % (op_group, op_code, pkt_len)
        packet = packet.upper()

        op_group_hex=0xA3
        op_code_hex=0xE0

        cmdjson = packet2json(op_group_hex,op_code_hex, packet)

    mynet_type="ztc"

    print("\t\t " + packet + "\n")

    #TODO : -write command into db  
    ts = datetime.datetime.now().isoformat()
    mynet_type ="ztc" 
    self.lock_tables("write", ['confcommands'])
self.db.execute("INSERT INTO confcommands (network_id, ntype, timestamp, command) \
                              VALUES (%s,%s,%s,%s)", nid, mynet_type, ts, cmdjson)
    self.unlock_tables();

    # TODO: - open the /tmp/iztc file in append mode
    cmdfile = open('/tmp/iztc', 'a')
    #       - acquire a lock  "only for the DB case, it's easier"
    #       - write the packet 
    cmdfile.write(netid + "\t"+ mynet_type + "\t"+ ts + "\t"+  cmdjson +"\n");
    #       - release the lock "only for the DB case, it's easier"
    #       - close the file
    cmdfile.close()

    if command == 'RESTARTNWK':
        opcodegroupr = "A4"
        opcoder = "E0"      

    #Code for retrieving the MAC address of the node
    como_url = "".join(['http://options.como_address:', options.como_port,
                        '/', ztc_config, '?netid=', netid,
                        '&opcode_group=', opcodegroupr, 
                        '&opcode=', opcoder, '&start=-5m&end=-1s'])
    http_client = AsyncHTTPClient()
    response = yield tornado.gen.Task(http_client.fetch, como_url)

    ret = {}
    if response.error:
        ret['error'] = 'Error while retrieving unregistered sensors'
    else:
        for line in response.body.split("\n"):
            if line != "": 
                value = int(line.split(" ")[6])

    ret['response'] = value
    self.write(tornado.escape.json_encode(ret))
    self.finish()

    if value == "0":
        # TODO: handle Errors. It always return succesfully.
        #ret['error'] = "Error while sending the %s command!" % (command.upper())
        ret['success'] = "The %s command has been succesfully sent!" % (command.upper())
        self.write(tornado.escape.json_encode(ret))
    else:
    ret['error'] = "Error while sending the %s command!" % (command.upper())
我在开发者控制台中收到的错误如下:

未捕获的TypeError:无法读取null的属性“error”

在js函数中。为什么函数不知道“错误”或“成功”?问题出在哪里????我认为程序不会在处理程序中输入never,而是在js函数中被阻塞


非常感谢您的帮助。

FYI:your
self.finish()
之后的脚本永远不会产生结果。因为请求已完成,数据已发送。但是你能把你的龙卷风申请表寄出去吗?以确保您的请求使用正确的脚本。谢谢。你是说这个吗<代码>(r“/networks/([0-9]+)/send/*”,NetworkSendHandler)
class NetworkSendHandler(BaseHandler):
# Requires authentication 
@tornado.web.authenticated
def get(self, nid):
    # Get the command 
    command = self.get_argument('command').upper(); 

    # The dictionary to return
    ret = {}

    #Check if the command is available
    if command not in ['RESTARTNWK']:
        raise tornado.web.HTTPError(404, "Unknown command: " + str(command))


    #Command ZDP-RestartNwk.Request
    if command == 'RESTARTNWK':
        op_group = "A3"
        op_code = "E0"
        packet_meta = "*%s;%s;%s;#"
        pkt_len = hextransform(0, 2)

        packet = packet_meta % (op_group, op_code, pkt_len)
        packet = packet.upper()

        op_group_hex=0xA3
        op_code_hex=0xE0

        cmdjson = packet2json(op_group_hex,op_code_hex, packet)

    mynet_type="ztc"

    print("\t\t " + packet + "\n")

    #TODO : -write command into db  
    ts = datetime.datetime.now().isoformat()
    mynet_type ="ztc" 
    self.lock_tables("write", ['confcommands'])
self.db.execute("INSERT INTO confcommands (network_id, ntype, timestamp, command) \
                              VALUES (%s,%s,%s,%s)", nid, mynet_type, ts, cmdjson)
    self.unlock_tables();

    # TODO: - open the /tmp/iztc file in append mode
    cmdfile = open('/tmp/iztc', 'a')
    #       - acquire a lock  "only for the DB case, it's easier"
    #       - write the packet 
    cmdfile.write(netid + "\t"+ mynet_type + "\t"+ ts + "\t"+  cmdjson +"\n");
    #       - release the lock "only for the DB case, it's easier"
    #       - close the file
    cmdfile.close()

    if command == 'RESTARTNWK':
        opcodegroupr = "A4"
        opcoder = "E0"      

    #Code for retrieving the MAC address of the node
    como_url = "".join(['http://options.como_address:', options.como_port,
                        '/', ztc_config, '?netid=', netid,
                        '&opcode_group=', opcodegroupr, 
                        '&opcode=', opcoder, '&start=-5m&end=-1s'])
    http_client = AsyncHTTPClient()
    response = yield tornado.gen.Task(http_client.fetch, como_url)

    ret = {}
    if response.error:
        ret['error'] = 'Error while retrieving unregistered sensors'
    else:
        for line in response.body.split("\n"):
            if line != "": 
                value = int(line.split(" ")[6])

    ret['response'] = value
    self.write(tornado.escape.json_encode(ret))
    self.finish()

    if value == "0":
        # TODO: handle Errors. It always return succesfully.
        #ret['error'] = "Error while sending the %s command!" % (command.upper())
        ret['success'] = "The %s command has been succesfully sent!" % (command.upper())
        self.write(tornado.escape.json_encode(ret))
    else:
    ret['error'] = "Error while sending the %s command!" % (command.upper())